replacing comma's with newlines using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing comma's with newlines using sed
# 1  
Old 09-28-2008
replacing comma's with newlines using sed

Hi All,
silly question that I'm sure is easy to answer for a more experienced coder...

I have a file called test.txt containing the following text...

need, to, break, this, line, into, individual, lines

using sed, I'd like to make the file look like this...

need
to
break
this
line
into
individual
lines

here's the code I've tried

#!/bin/ksh
sed "s/,/\n/" test.txt > test.new.txt

but it returns the following in test.new.txt
Addn this, line, after, every, line, with, WORD


so, my questions are...
1.) how can I make sed replace all instances of "," in a given line?
2.) how can I make sed insert newline characters instead of just the character n?

Thanks in advance!
# 2  
Old 09-28-2008
Quote:
Originally Posted by newbie_coder
Hi All,
silly question that I'm sure is easy to answer for a more experienced coder...

I have a file called test.txt containing the following text...

need, to, break, this, line, into, individual, lines

using sed, I'd like to make the file look like this...

need
to
break
this
line
into
individual
lines

here's the code I've tried

#!/bin/ksh
sed "s/,/\n/" test.txt > test.new.txt

but it returns the following in test.new.txt
Addn this, line, after, every, line, with, WORD


so, my questions are...
1.) how can I make sed replace all instances of "," in a given line?
2.) how can I make sed insert newline characters instead of just the character n?

Thanks in advance!
Code:
d@DeCobox-Micro ~
$ cat unix 
need, to, break, this, line, into, individual, lines

$ cat unix |sed 's/\,/\n/g'
need
 to
 break
 this
 line
 into
 individual
 lines

d@DeCobox-Micro ~
$ cat unix |tr "," "\n"
need
 to
 break
 this
 line
 into
 individual
 lines

d@DeCobox-Micro ~
$ cat unix |tr "," "\n" |tr -d "^ "
need
to
break
this
line
into
individual
lines

This User Gave Thanks to DeCoTwc For This Post:
# 3  
Old 09-28-2008
See if this works for you:

Code:
cat test.txt | sed 's/, /\n/g'

Don't forget to add the space after the coma otherwise you will end up with spaces on the new lines.


Vic.
# 4  
Old 09-28-2008
If \n don't work try:
Code:
$ echo 'foo, bar' | sed 's/, /\
/g'
foo
bar

Or awk:
Code:
$ echo 'foo, bar' | awk -F', ' '{for(i=1;i<=NF;i++) printf "%s\n", $i}'

This User Gave Thanks to danmero For This Post:
# 5  
Old 09-28-2008
Quote:
Originally Posted by newbie_coder
Hi All,
silly question that I'm sure is easy to answer for a more experienced coder...

I have a file called test.txt containing the following text...

need, to, break, this, line, into, individual, lines

using sed, I'd like to make the file look like this...

need
to
break
this
line
into
individual
lines

here's the code I've tried

#!/bin/ksh
sed "s/,/\n/" test.txt > test.new.txt

but it returns the following in test.new.txt
Addn this, line, after, every, line, with, WORD


so, my questions are...
1.) how can I make sed replace all instances of "," in a given line?
2.) how can I make sed insert newline characters instead of just the character n?

Thanks in advance!
the basic algorithm/steps to do this, is to split the line on "," and then print them out with newline
Code:
for line in open("file"):
    line = line.split(",")
    for items in line:
        print items

# 6  
Old 09-29-2008
Thanks good one
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replacing required newlines

hi i have a requirement to replace a string with another using sed and to get the result newline separated but after sed replacement the newline vanishes below is sample code #!/bin/ksh set -x string="name sam\nage 45 \nsport soccer" echo $string string=`echo $string | sed... (2 Replies)
Discussion started by: midhun19
2 Replies

2. Shell Programming and Scripting

Need help with Sed (replacing parenthesis and comma)

I have the following text as an input text: input.txt Results('Toilet', 'Sink', ) and i want to remove the last comma so the output is output.txt Results('Toilet', 'Sink' ) I tried using the following sed command, but I get a parsing error: sed s/, \)/\)/g input.txt >... (5 Replies)
Discussion started by: jl487
5 Replies

3. Shell Programming and Scripting

How to split the 11 comma's with number into newlines?

Hi all, This my requirement is to spilt the comma's into new line my sample is Name,india,ID,cost,Date,vadaloreOffset,neyveliCurveUnit,Riskcuddalore ,,,,,,,,,,,1,0.00576652,,,,,,,,,,,7,0.00625467,,,,,,,,,,,30,0.00832759,,,,,,,,,,,61,0.00977132 expected output to be like this... (2 Replies)
Discussion started by: koviraja
2 Replies

4. UNIX for Dummies Questions & Answers

Replacing | with a comma

I have a huge file which is pipe delimiter and i want to replace the pipe delimiter to a comma Please Help as its v urgent. Ex: parent|child|alias|....Heading of the file...and the data is of similar structure. (4 Replies)
Discussion started by: win4luv
4 Replies

5. Shell Programming and Scripting

sed help - replacing 6th comma with a space

Hi, How can I replace the 6th comma on each line (of a csv) with a space? Any online tutorials with plenty of examples using sed would be very useful. Alex (2 Replies)
Discussion started by: mcclunyboy
2 Replies

6. UNIX for Dummies Questions & Answers

adding symbol to newlines with out replacing them

Guyz I have been using tr command to replace symbol. I would like to add a symbol to all newlines in a textfile with out replacing them input \n (i mean new line) a \n b \n c output > a > b > c (0 Replies)
Discussion started by: repinementer
0 Replies

7. Shell Programming and Scripting

Replacing dot for comma

I wanted to change 34.66 to 34,66. I tried the command: sed 's/./,/' $NUM Where $NUM is a variable with 34.66 value. The output is ,4.66 (2 Replies)
Discussion started by: bdalmeida
2 Replies

8. UNIX for Dummies Questions & Answers

Replacing Comma by Tab

Hi All, i have a file test.txt as shown below, 1,test,test111 2,rest,rest222 i want to replace the commas by tab delimiter.., it should be like, 1 test test111 2 rest rest222 i tried the following code, sed 's/,/\\t/g' test.txt >> ouptut.txt (9 Replies)
Discussion started by: Serious Sam
9 Replies

9. Shell Programming and Scripting

replacing strings with newlines : sed

Hi everyone, Since the previous time I received help from unix.com I have been encouraged to learn more. going through 1 of the articles(View Article) on sed I found, it pointed an interesting situation. Suppose the text is : Romeo and Ethel the Dancer Moves Audience to Tears. I... (3 Replies)
Discussion started by: hkansal
3 Replies
Login or Register to Ask a Question