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:

/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:

$ /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

do not escape the \ for \n

use sed 's/:/\n/'

This worked for me

tr ":" "\n" < longString.txt
/usr/bin/sed 's/:/\\
/g' longString.txt > readableString.txt
tr ':' '\n' < longString.txt > readableString.txt
awk -F":" -v OFS="\n" ' $1=$1 ' longString.txt > readableString.txt

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:

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:

$ /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:

$ /usr/bin/sed 's/:/\\
> /g' longString.txt > readableString.txt
sed: command garbled: s/:/\\

(or:

sed: Ending delimiter missing on substitution: s/:/\\

depending on sed variant).

Use double quotes

/usr/bin/sed "s/:/\\
/g" longString.txt > readableString.txt

If you have perl

perl -ne ' s/:/\n/g; print ' longString.txt > readableString.txt

Thanks anbu23, those work as well :slight_smile:

Do you have any idea why the somewhat simpler sed and tr commands won't work for me?

I am not sure about tr command. But what about sed command. Did you try using double quotes?

Yes, the last sed command you posted with double quotes worked fine. But sed replacements using \n as line break don't work.

It turns out that the default sed and tr on my system are probably quite old, since /usr/ucb was before /usr/bin and /bin in my PATH. When using a different tr, \n as replacement worked. When using the default (/usr/ucb/tr), it only works if the replacement is \012 instead of \n. However, \012 won't work at all with sed. I don't quite understand this - perhaps it is the result of a bad combination of tr (or sed) version and type/version of shell?

Is there a way to get sed and/or tr to print which version it is?

# awk -F":" '{ for (i=1;i<=NF;i++) print $i}' file
/home/user/tmp/myDir/file1.log
/home/user/tmp/myOldDir/file3.txt
/usr/local/java/bin/javac

u can use sed command as below:

sed 's/:/\n/g' filename.
this will break all the tokens (:slight_smile: into a new line char.Try this