![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 05: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 02:17 PM |
| Replacing all but last Hex characters in a text line | BAH | Shell Programming and Scripting | 2 | 03-26-2004 01:00 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
do not escape the \ for \n
use sed 's/:/\n/' |
|
#3
|
||||
|
||||
|
This worked for me
Code:
tr ":" "\n" < longString.txt |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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 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 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/:/\\ Code:
sed: Ending delimiter missing on substitution: s/:/\\ |
|
#6
|
|||
|
|||
|
Use double quotes
Code:
/usr/bin/sed "s/:/\\ /g" longString.txt > readableString.txt Code:
perl -ne ' s/:/\n/g; print ' longString.txt > readableString.txt |
|
#7
|
|||
|
|||
|
Thanks anbu23, those work as well
Do you have any idea why the somewhat simpler sed and tr commands won't work for me? |
|||
| Google The UNIX and Linux Forums |