The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 05:25 AM
replacing the characters in a file trichyselva UNIX for Dummies Questions & Answers 2 01-03-2008 08:02 AM
replacing certain characters with new line? Bashar Shell Programming and Scripting 4 05-13-2007 04:34 PM
replacing few characters in a file purnakarthik UNIX for Dummies Questions & Answers 1 01-25-2007 05:17 PM
Replacing all but last Hex characters in a text line BAH Shell Programming and Scripting 2 03-26-2004 04:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-24-2007
johnemb johnemb is offline
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
  #2 (permalink)  
Old 04-24-2007
napster_san napster_san is offline
Registered User
  
 

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

use sed 's/:/\n/'
  #3 (permalink)  
Old 04-24-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

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

Code:
tr ":" "\n" < longString.txt
  #4 (permalink)  
Old 04-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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
  #5 (permalink)  
Old 04-24-2007
johnemb johnemb is offline
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).
  #6 (permalink)  
Old 04-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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
  #7 (permalink)  
Old 04-24-2007
johnemb johnemb is offline
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?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0