Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tr cmd - how to use sed / awk instead to insert cntl chars

Status
Not open for further replies.

pauljt

Technical User
Feb 26, 2003
31
GB
Hi,

For a 'script' I have put together I needed to get the
PATH variable - and convert the contents of it from:

/usr/dt/bin:/usr/openwin/bin.... etc

to a file...

/usr/dt/bin
/usr/openwin/bin

I have achieved this using:

echo $PATH | tr ":" "\012"

This is absolutely fine, but how could you do this
with sed or awk instead?

I was struggling to work out how to handle control characters in those two beasts...

Thanks!
 
Code:
home/jp> echo $PATH | sed -e "s/:/\> /g"



Jean Pierre.
 
You should be able to enter control characters in the same way as you would in vi...
[tt]
echo $PATH|sed 's/:/\^J/g'
[/tt]
where ^J is ctrl V ctrl J

Using awk...
[tt]
echo $PATH|awk '{gsub(":","\012");print}'
[/tt]
 
Ygor, sed doesn't accept this syntax :
[tt]
home/jp> echo $PATH | sed -e s/:/^J/g'
sed: Function s/:/ cannot be parsed.
[/tt]


Jean Pierre.
 
Sorry, missing
With a single \, ':' is replaced par nothing ''
You must specify \\^J



Jean Pierre.
 
echo $PATH|awk -F":" '{ for (i = 1;i <= NF;i++) print $i }'
 
yet another method .... ;-)

echo $PATH | tr ':' '\n'


Jean Pierre.
 
Jean Pierre,
You only need to escape the backslash if you are using double quotes...
[tt]
echo $PATH|sed "s/:/\\^J/g"
[/tt]
...is equivalent to...
[tt]
echo $PATH|sed 's/:/\^J/g'
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top