Replacing characters in file with line break


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing characters in file with line break
# 1  
Old 04-24-2007
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  
Old 04-24-2007
do not escape the \ for \n

use sed 's/:/\n/'
# 3  
Old 04-24-2007
This worked for me

Code:
tr ":" "\n" < longString.txt

# 4  
Old 04-24-2007
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  
Old 04-24-2007
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  
Old 04-24-2007
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  
Old 04-24-2007
Thanks anbu23, those work as well Smilie

Do you have any idea why the somewhat simpler sed and tr commands won't work for me?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replacing characters in a file

Suppose I have a file which has 1000 columns (5 SHOWN FOR EXAMPLE) two alphabets are separated by a space and then tab A A"\t"C C"\t"G G"\t"0 0"\t"T T A G"\t"C C"\t"G G"\t"A T"\t"0 0 G A"\t"0 0"\t"G C"\t"A A"\t"T C whenever there is a 0 0 in any column, the output should be printed as A... (12 Replies)
Discussion started by: rossi
12 Replies

2. Shell Programming and Scripting

Replacing end of line characters

Hello, I'm looking for some help in renaming data-timestamps stored within different calendar directories/files. The calendar directory has hundreds of ICS files: ~/Library/Calendars/F494C908.calendar/Events/92B65E439AAC.ics ~/Library/Calendars/F494C908.calendar/Events/DE7867E61969.ics... (8 Replies)
Discussion started by: Schubi
8 Replies

3. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

4. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

5. Shell Programming and Scripting

Break line after last "/" if length > X characters

Hello. I am a french newbie in unix shell scripting (sorry if my english speaking is wrong). I have a file with path and filenames in it. I want to limit the number of characters on each line and break the line if necessary. But the "break" should occur after a slash caracter "/". Example of... (9 Replies)
Discussion started by: SportBilly
9 Replies

6. UNIX for Dummies Questions & Answers

Replacing characters in csv file

Hello all, This is my first post here, so please excuse me if this question is too obvious or has been asked before. I am new to Unix and although I tried to search your forum for the answer to my question, I could not find an answer that would help me. I have a 500MB csv file with numeric values... (1 Reply)
Discussion started by: finwhiz
1 Replies

7. UNIX for Dummies Questions & Answers

replacing the characters in a file

hi i want to replace the characters between positions 2 to 30 in each line in a file how to do it suggestions welcome (2 Replies)
Discussion started by: trichyselva
2 Replies

8. Shell Programming and Scripting

replacing certain characters with new line?

i have a text file which domains something like this 123213213213/32434342 324324324/12312321321 12321321,435435435 12321312 / 12313213 / 12435435345 4353213 , 123213213213 21321321312-12334324 234324324 - 235645645645 456456456 - 45456456456 - 45645645654243 how can i replace '/' and... (4 Replies)
Discussion started by: Bashar
4 Replies

9. UNIX for Dummies Questions & Answers

replacing few characters in a file

Hi All, I have huge xml file. The file contains some comment tags . I have requirement to replace comment tag with another comment tag. Say for example : file X has -- Part of the file <?xml version="1.0" encoding="ISO-2"?><translationResults jobDate="20070123 23:20:51"... (1 Reply)
Discussion started by: purnakarthik
1 Replies

10. Shell Programming and Scripting

Replacing all but last Hex characters in a text line

I must remove hex characters 0A and 0D from several fields within an MS Access Table. Since I don't think it can be done in Access, I am trying here. I am exporting a Table from Access (must be fixed length fields, I think, for my idea to work here) into a text format. I then want to run a... (2 Replies)
Discussion started by: BAH
2 Replies
Login or Register to Ask a Question