LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help with sed command (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-sed-command-719638/)

cbtsig 04-16-2009 01:40 PM

need help with sed command
 
Hi guys. I've been trying to figure out sed for some time with no success. The man pages are confusing at best and I've scoured the internet for help. I've read a few tutorials and getting started guides and things like that but I'm unable to get the right command to do what I want. All I want to do is take a list of IP addresses that are on consecutive lines and print them on the same line separated by commas to a new file. eg:

file "IP1.txt" looks like

1.2.3.4
5.6.7.8
4.3.2.1
8.7.6.5

and I want them to output to "IP2.txt" and look like this:

1.2.3.4, 5.6.7.8, 4.3.2.1, 8.7.6.5

If anyone can help me out with a sed one liner I would really appreciate it and you'd be saving me some more frustration trying to figure it out.

Thanks for your help.

acid_kewpie 04-16-2009 02:23 PM

this isn't really something sed is good at without some meddling around. Sed works on data on a line by line basis, so you can't remove the carriage returns as sed never sees them. It's possible to mess with bash environmental variables to change what defines a new record, but this just isn't a good way to learn sed.

I'd use tr instead, which isn't line based... "cat file | tr '\n' ','"

pixellany 04-16-2009 02:27 PM

Quote:

Originally Posted by cbtsig (Post 3511296)
I've scoured the internet for help.

Did you find the best tutorial??---here:
http://www.grymoire.com/Unix/Sed.html

I agree with acid that SED is not the best choice, but it is possible. Look at the commands for reading and writing to/from the hold buffer..

-Merlin- 04-16-2009 02:30 PM

Why sed?


Use tr instead (unless you want to dive ni sed a little deeper:)) :


cat something|tr "\n" ","


M.

MensaWater 04-16-2009 02:35 PM

As the others have said (sed?) the best way to do this isn't with sed.

This link however, does show a way to do it with sed that looks quite cryptic. It also gives examples with tr, awk and perl.

http://linux.dsplabs.com.au/rmnl-rem...-sam-ssam-p65/

cbtsig 04-16-2009 02:46 PM

I don't really know why sed!! hence the linux newbie forum! : )
 
Thanks everyone for the responses... the 'cat file | tr "\n" ","' command worked perfectly! I used to work with someone who accomplished this with 'sed', but lost the command he used. He was considerably smarter than me on computers. This command is so simple! I really appreciate the help! Thanks again!

cmdln 04-16-2009 02:58 PM

Quote:

Originally Posted by cbtsig (Post 3511296)
file "IP1.txt" looks like

1.2.3.4
5.6.7.8
4.3.2.1
8.7.6.5

and I want them to output to "IP2.txt" and look like this:

1.2.3.4, 5.6.7.8, 4.3.2.1, 8.7.6.5

If anyone can help me out with a sed one liner I would really appreciate it and you'd be saving me some more frustration trying to figure it out.

Thanks for your help.

This should work.
Code:

cp IP1.txt IP2.txt; sed -i 's/\n/, /g' IP2.txt

acid_kewpie 04-16-2009 03:26 PM

no, it won't because sed is a line editor not a stream editor. Despite the s.

this DOES work... sed ':a;N;$!ba;s/\n/, /g' IP1.txt

but that's horrible, and *I* don't understand what is being done there... as above, not a good basic teaching question.

http://www.thekramms.com/Oreilly/boo...wk/ch06_01.htm

cmdln 04-16-2009 06:09 PM

Quote:

Originally Posted by acid_kewpie (Post 3511401)
no, it won't because sed is a line editor not a stream editor.

Ah you are right, I didn't even test it. Well i will toss in another solution that does work.

Code:

for i in $(cat IP1.txt);do echo -n "$i, " >> IP2.txt;done

jschiwal 04-16-2009 07:08 PM

It doesn't look that bad if you break it up.
Code:

sed ':a
      N
      $!ba
      s/\n/, /g' IP1.txt

But I would use `tr' to simply join lines. Sometimes, even just after a sed filter.

ghostdog74 04-16-2009 07:52 PM

Quote:

Originally Posted by cbtsig (Post 3511367)
'cat file | tr "\n" ","'

no need cat
Code:

tr "\n" ","'  < file
awk
Code:

awk '{$1=$1}1' RS="" OFS="," file
1.2.3.4,5.6.7.8,4.3.2.1,8.7.6.5



All times are GMT -5. The time now is 05:32 PM.