f



[Q] How to escape "\a" "\b" "\c" "\f" "\n" "\r" "\t" "\v" in the string output?

Hi, there.

Belows are some example strings which stand for M$ Windows directory
path. How can I escape the special meanings like "\a", "\n", "\r" in
these M$ Windows directory name strings?

C:\amail\dir1\nbar
C:\bmail\dir2\tbar
C:\cmail\dir3\afoo
C:\fmail\dir4\vfoo
C:\nmail\dir5\fbar
C:\rmail\dir6\abar
C:\tmail\dir7\vfoo
C:\vmail\dir8\cfoo


Thanks in advance.

0
yh
10/18/2006 4:25:22 AM
comp.unix.shell 15484 articles. 3 followers. Post Follow

9 Replies
1608 Views

Similar Articles

[PageSpeed] 28

In article <1161145522.072002.20370@m7g2000cwm.googlegroups.com>,
 "yh" <y.hosang@gmail.com> wrote:

> Hi, there.
> 
> Belows are some example strings which stand for M$ Windows directory
> path. How can I escape the special meanings like "\a", "\n", "\r" in
> these M$ Windows directory name strings?
> 
> C:\amail\dir1\nbar
> C:\bmail\dir2\tbar
> C:\cmail\dir3\afoo
> C:\fmail\dir4\vfoo
> C:\nmail\dir5\fbar
> C:\rmail\dir6\abar
> C:\tmail\dir7\vfoo
> C:\vmail\dir8\cfoo

Escape the backslash: \\

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Barry
10/18/2006 4:35:28 AM
How can I escape the backslash in the stream?

$ echo "C:\nmail"
C:
mail
$ echo "C:\\nmail"
C:
mail
$ echo "C:\\\nmail"
C:\nmail
$ echo "C:\nmail" | sed -e 's/\\n/\\\n/g'
C:
mail



Barry Margolin wrote:
> In article <1161145522.072002.20370@m7g2000cwm.googlegroups.com>,
>  "yh" <y.hosang@gmail.com> wrote:
>
> > Hi, there.
> >
> > Belows are some example strings which stand for M$ Windows directory
> > path. How can I escape the special meanings like "\a", "\n", "\r" in
> > these M$ Windows directory name strings?
> >
> > C:\amail\dir1\nbar
> > C:\bmail\dir2\tbar
> > C:\cmail\dir3\afoo
> > C:\fmail\dir4\vfoo
> > C:\nmail\dir5\fbar
> > C:\rmail\dir6\abar
> > C:\tmail\dir7\vfoo
> > C:\vmail\dir8\cfoo
>
> Escape the backslash: \\
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

0
yh
10/18/2006 4:49:34 AM
In article <1161146974.238989.78960@e3g2000cwe.googlegroups.com>,
 "yh" <y.hosang@gmail.com> wrote:

> How can I escape the backslash in the stream?
> 
> $ echo "C:\nmail"
> C:
> mail
> $ echo "C:\\nmail"
> C:
> mail
> $ echo "C:\\\nmail"
> C:\nmail
> $ echo "C:\nmail" | sed -e 's/\\n/\\\n/g'
> C:
> mail

Don't use echo.

printf "%s\n" "C:\nmail"

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Barry
10/18/2006 5:07:40 AM
What if I have to use echo, or send the string with pipe?


Barry Margolin wrote:
> In article <1161146974.238989.78960@e3g2000cwe.googlegroups.com>,
>  "yh" <y.hosang@gmail.com> wrote:
>
> > How can I escape the backslash in the stream?
> >
> > $ echo "C:\nmail"
> > C:
> > mail
> > $ echo "C:\\nmail"
> > C:
> > mail
> > $ echo "C:\\\nmail"
> > C:\nmail
> > $ echo "C:\nmail" | sed -e 's/\\n/\\\n/g'
> > C:
> > mail
>
> Don't use echo.
>
> printf "%s\n" "C:\nmail"
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

0
yh
10/18/2006 7:15:36 AM
In article <1161155736.048016.233190@h48g2000cwc.googlegroups.com>,
yh <y.hosang@gmail.com> wrote:
>What if I have to use echo, or send the string with pipe?

What if you're a useless, top-posting tool?

0
gazelle
10/18/2006 10:37:46 AM
"yh" <y.hosang@gmail.com> writes:

> What if I have to use echo, or send the string with pipe?

Edit the contents before you echo/pipe it.

-- 
Sending unsolicited commercial e-mail to this account incurs a fee of 
$500 per message, and acknowledges the legality of this contract.
0
Bruce
10/18/2006 11:08:54 AM
At 2006-10-18 12:49AM, "yh" wrote:
>  How can I escape the backslash in the stream?
>  
>  $ echo "C:\nmail"
>  C:
>  mail
>  $ echo "C:\\nmail"
>  C:
>  mail
>  $ echo "C:\\\nmail"
>  C:\nmail
>  $ echo "C:\nmail" | sed -e 's/\\n/\\\n/g'
>  C:
>  mail

you might try the more general case:  echo "..." | sed -e 's/\\/\\\\/g'

-- 
Glenn Jackman
Ulterior Designer
0
Glenn
10/18/2006 1:56:31 PM
Glenn Jackman <glennj@ncf.ca> writes:

>>  $ echo "C:\nmail" | sed -e 's/\\n/\\\n/g'
>>  C:
>>  mail
>
> you might try the more general case:  echo "..." | sed -e 's/\\/\\\\/g'


Ah.... Are you sure?

If the echo command doesn't output a "\", then what will the sed command do?


-- 
Sending unsolicited commercial e-mail to this account incurs a fee of 
$500 per message, and acknowledges the legality of this contract.
0
Bruce
10/19/2006 12:05:56 AM
yh wrote:
> How can I escape the backslash in the stream?
> 
> $ echo "C:\nmail"
> C:
> mail
> $ echo "C:\\nmail"
> C:
> mail
> $ echo "C:\\\nmail"
> C:\nmail
> $ echo "C:\nmail" | sed -e 's/\\n/\\\n/g'
> C:
> mail
> 

Not pretty, but it uses echo ...

sample="C:\nmail"
saveifs="$IFS"
IFS='\'
for piece in $sample
do
	echo "$piece"'\\'"\c"
done
echo
IFS="$saveifs"
0
Geoff
10/19/2006 1:35:35 AM
Reply: