What is wrong with: echo $PATH | sed s/:/\\n/g


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What is wrong with: echo $PATH | sed s/:/\\n/g
# 8  
Old 07-16-2013
Your sed is failing because most sed implementations do not support the \n escape sequence in the replacement text of the substitution command.

A couple of simple, portable alternatives:
Code:
sed 'y/:/\n/'

Code:
tr : '\n'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 9  
Old 07-16-2013
This works for me.
Code:
tcsh --version
tcsh 6.18.01 (Astron) 2012-02-14 (unknown-apple-darwin) options wide,nls,dl,al,kan,rh,color,filec

echo $PATH | sed "s/:/\\
? /g"
/usr/local/bin
/usr/local/sbin
/opt/local/bin
/opt/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin
/usr/X11/bin

# 10  
Old 07-16-2013
Quote:
Originally Posted by marek
Code:
echo $PATH | sed 's/:/\n/g'
echo $PATH | sed 's/:/"\n"/g'
echo $PATH | sed 's/:/\\n/g'
echo $PATH | sed 's/:/"\\n"/g'
echo $PATH | sed "s/:/\n/g"
echo $PATH | sed "s/:/\\n/g"

hmm something wrong with my shell ...
No, There isn't. The problem is that you don't realize that most sed implementations do not support a \n escape sequence in the replacement text of a substitution command.

Quote:
Originally Posted by marek
Now lets try with a tab:

Code:
echo $PATH | sed 's/:/\t/g'
echo $PATH | sed 's/:/\\t/g'
echo $PATH | sed "s/:/\\t/g"
echo $PATH | sed "s/:/\t/g"

That means my shell has problems to interpret well the '\'
No. As with \n, most sed implementations do not support a \t escape sequence in the replacement text of a substitution command.

Quote:
Originally Posted by marek
No lets try your suggestion:

Code:
echo $PATH | sed "s/:/\
? /g"
sed: 1: "s/:/
/g": unescaped newline inside substitute pattern

This is due to how the shell treats a backslash at the end of a line within a double quoted string.

Quote:
Originally Posted by marek
same for 's ... '

Code:
echo $PATH | sed 's/:/\                                                                                 /g'
sed: 1: "s/:/
/g": unescaped newline inside substitute pattern

You made a mistake, because within single quotes, a backslash is not special and if it immediately precedes the newline then sed will be happy.

You should spend some time with both your shell and sed documentation.

Regards,
Alister

P.S. Above, when I say "most", I mean all except GNU sed.
# 11  
Old 07-16-2013
Thank you allister!


I learned a lot! Yes I will do my homeworks now an read the manuals ...

this code is working:

Code:
echo $PATH | sed 'y/:/\n/'

hurray!

suppose the y/// is something like tr/// in Perl? Yes yes I will do my homeworks and find the answer myself.


marek


ps: I even updated my tcsh to tcsh 6.18.01 (Astron) 2012-02-14 now!

hey xbin!

I just read your answer now! You are on Mac and you have the same tcsh. But your suggestion is not working here! Really strange!
# 12  
Old 07-16-2013
tcsh has many design flaws, such as this one. I suggest using any shell but tcsh, where strings containing newlines will be valid instead of syntax errors.
# 13  
Old 07-17-2013
I'd like to question your presuppositions: why do you need to put newlines into your PATH variable? If it is because you want to work on its parts in a loop of some sort this might also work for you, without the need for "sed" or any other tool:

Code:
IFS=":" for var in $PATH ; do
     echo var is: \"$var\"
done

I hope this helps.

bakunin
# 14  
Old 07-17-2013
In tcsh you would take the corresponding $path that is in list format
Code:
foreach var ( $path:q )
 echo "$var"
end


Last edited by MadeInGermany; 07-17-2013 at 05:01 AM.. Reason: :q is safer
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find the wrong filename in a path and raise a trap for it

Example: I have server name A with an IP : 125.252.235.455 I have an username /password to login into this server under SSH connection In this server i have a path /apps/user/filename(Big.txt) Everyday we used to get the filename as Big.txt. I want a shell script to monitor this path... (4 Replies)
Discussion started by: ChandruBala73
4 Replies

2. Shell Programming and Scripting

What's wrong with my sed?

lyang0@lyang0-OptiPlex-755:~$ cat xx Settings for eth1: Supported ports: Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half... (11 Replies)
Discussion started by: yanglei_fage
11 Replies

3. Shell Programming and Scripting

What's wrong my sed?

$ cat file1 gcc-configure-sdk, gcc-configure-cross: Dont recomput libgcc: Remove unpackage unwind.h $ cat file2 fc94926 gcc-configure-sdk, gcc-configure-cross: Dont recomput f7ec6ea libgcc: Remove unpackage unwind.h f800988 balbababababa $ cat ./xx.sh #!/bin/bash while read... (10 Replies)
Discussion started by: yanglei_fage
10 Replies

4. UNIX for Dummies Questions & Answers

echo windows path

I have developed a script that transfers files from a UNIX machine to a Windows machine. Transferring the files is working perfectly, but my echo statements are displaying the destination (Windows) path names incorrectly. I understand that it is the "\" that is causing this, but is there anyway... (5 Replies)
Discussion started by: shammah77
5 Replies

5. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

6. AIX

Wrong path resolution

lspath out: Enabled hdisk14 fscsi0 Enabled hdisk15 fscsi0 Enabled hdisk14 fscsi0 Enabled hdisk15 fscsi0 Enabled hdisk0 fscsi1 Enabled hdisk1 fscsi1 Enabled hdisk2 fscsi1 Enabled hdisk3 fscsi1 Enabled hdisk4 fscsi1 Enabled hdisk5 fscsi1 Enabled hdisk6 fscsi1 Enabled hdisk7 ... (1 Reply)
Discussion started by: federico1636
1 Replies

7. Shell Programming and Scripting

SH echo$path

I need to run my shell script just by typing its name rather than doing sh <scrpit name>. I think its something to do with my profile and path. echo $path is giving me output as follows: sr/games /opt/gnome/bin /opt/kde3/bin /usr/bin/X11 /usr/local/bin /usr/bin /bin /usr/sbin /sbin... (3 Replies)
Discussion started by: 1MB
3 Replies

8. UNIX for Dummies Questions & Answers

echo $PATH doesn't match $HOME/.profile

This is on a Solaris 9 box, but I feel like a noob, so I am posting here. When I echo $PATH I get a lot of duplicate paths and extra stuff I don't need. What I want is just what I set up in my home dir under .profile My login shell=/bin/bash I checked the following and there are no path... (1 Reply)
Discussion started by: Veestan
1 Replies

9. UNIX for Dummies Questions & Answers

What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty. TEST= if ${TEST:?} ; then echo empty; fi But it doesn't work. What's wrong? (2 Replies)
Discussion started by: meili100
2 Replies

10. Shell Programming and Scripting

SED - How to return PATH from PATH/NAME

Hi, How can I get /usr/people/me/ from /usr/people/me/file.abc with sed? Thanks... (2 Replies)
Discussion started by: cybotic
2 Replies
Login or Register to Ask a Question