The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing characters in csv file finwhiz UNIX for Dummies Questions & Answers 1 03-31-2008 02:25 AM
replacing the characters in a file trichyselva UNIX for Dummies Questions & Answers 2 01-03-2008 04:02 AM
replacing certain characters with new line? Bashar Shell Programming and Scripting 4 05-13-2007 01:34 PM
replacing few characters in a file purnakarthik UNIX for Dummies Questions & Answers 1 01-25-2007 01:17 PM
Replacing all but last Hex characters in a text line BAH Shell Programming and Scripting 2 03-26-2004 12:00 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2007
Registered User
 

Join Date: Nov 2005
Posts: 4
Replacing characters in file with line break

Hi,

Apologies if this has been asked before, but I searched and was not able to find an answer. It's probably a simple question to answer for those of you with some experience, though...

I have a relatively long string where tokens are separated by the colon (':') character. Let's say the string is stored in a file, 'longString.txt'. The string may look something like this:

Code:
/home/user/tmp/myDir/file1.log:/home/user/tmp/myOldDir/file3.txt:/usr/local/java/bin/javac
I want to separate each token with a line break instead of a colon, so that long strings are easy to read for humans. How do I do this easily from a (bash) shell command line?

I tried with tr and sed, but even if I get to replace ':' with '\n', the '\n' is apparently not interpreted as a line break in the resulting output. Sed example:

Code:
$ /usr/bin/sed 's/:/\\n/g' longString.txt > readableString.txt
$ cat readableString.txt
/home/user/tmp/myDir/file1.log\n/home/user/tmp/myOldDir/file3.txt\n/usr/local/java/bin/javac
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-24-2007
Registered User
 

Join Date: Mar 2007
Posts: 6
do not escape the \ for \n

use sed 's/:/\n/'
Reply With Quote
  #3 (permalink)  
Old 04-24-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
This worked for me

Code:
tr ":" "\n" < longString.txt
Reply With Quote
  #4 (permalink)  
Old 04-24-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
/usr/bin/sed 's/:/\\
/g' longString.txt > readableString.txt
Code:
tr ':' '\n' < longString.txt > readableString.txt
Code:
awk -F":" -v OFS="\n" ' $1=$1 ' longString.txt > readableString.txt
Reply With Quote
  #5 (permalink)  
Old 04-24-2007
Registered User
 

Join Date: Nov 2005
Posts: 4
Thank you all for such quick responses!

I tried all the suggestions, but of the ones listed so far only the last one from anbu23 works for me:

Code:
awk -F":" -v OFS="\n" ' $1=$1 ' longString.txt > readableString.txt
but it only works with one of the three awk binaries installed on my system (I don't have control over what is actually installed).

Could this have something to do with for example the version of sed and tr I am using (I'm not sure which version that is)? Or my environment settings (nothing special there I think)? I'm running Solaris 10 and the Bash shell in a networked environment. Some other results:

Unescaped '\n' with sed:
Code:
$ /usr/bin/sed 's/:/\n/g' longString.txt
/home/user/tmp/myDir/file1.logn/home/user/tmp/myOldDir/file3.txtn/usr/local/java/bin/javac
(I also tried other sed variants installed on my system)

Vino's and anbu23's suggestions using tr gives the same results for me as the above sed command.

anbu23's sed command:
Code:
$ /usr/bin/sed 's/:/\\
> /g' longString.txt > readableString.txt
sed: command garbled: s/:/\\
(or:
Code:
sed: Ending delimiter missing on substitution: s/:/\\
depending on sed variant).
Reply With Quote
  #6 (permalink)  
Old 04-24-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Use double quotes
Code:
/usr/bin/sed "s/:/\\
/g" longString.txt > readableString.txt
If you have perl
Code:
perl -ne ' s/:/\n/g; print ' longString.txt > readableString.txt
Reply With Quote
  #7 (permalink)  
Old 04-24-2007
Registered User
 

Join Date: Nov 2005
Posts: 4
Thanks anbu23, those work as well

Do you have any idea why the somewhat simpler sed and tr commands won't work for me?
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0