LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2009, 02:40 PM   #1
cbtsig
LQ Newbie
 
Registered: Jan 2005
Posts: 3

Rep: Reputation: 0
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.
 
Old 04-16-2009, 03:23 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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' ','"
 
Old 04-16-2009, 03:27 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by cbtsig View Post
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..

Last edited by pixellany; 04-17-2009 at 01:55 PM. Reason: corrected typo
 
Old 04-16-2009, 03:30 PM   #4
-Merlin-
LQ Newbie
 
Registered: Apr 2009
Distribution: Ubuntu and Slackware
Posts: 7

Rep: Reputation: 0
Why sed?


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


cat something|tr "\n" ","


M.
 
Old 04-16-2009, 03:35 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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/
 
Old 04-16-2009, 03:46 PM   #6
cbtsig
LQ Newbie
 
Registered: Jan 2005
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
Old 04-16-2009, 03:58 PM   #7
cmdln
Member
 
Registered: Apr 2009
Location: Lawrence, KS
Distribution: Debian, Centos
Posts: 102
Blog Entries: 1

Rep: Reputation: 24
Quote:
Originally Posted by cbtsig View Post
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
 
Old 04-16-2009, 04:26 PM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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

Last edited by acid_kewpie; 04-16-2009 at 04:28 PM.
 
Old 04-16-2009, 07:09 PM   #9
cmdln
Member
 
Registered: Apr 2009
Location: Lawrence, KS
Distribution: Debian, Centos
Posts: 102
Blog Entries: 1

Rep: Reputation: 24
Quote:
Originally Posted by acid_kewpie View Post
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
 
Old 04-16-2009, 08:08 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.

Last edited by jschiwal; 04-16-2009 at 08:21 PM.
 
Old 04-16-2009, 08:52 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by cbtsig View Post
'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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use sed command raghuvamsi Linux - General 2 06-26-2008 02:17 AM
sed command Fond_of_Opensource Linux - Newbie 2 11-17-2006 09:11 AM
sed command ancys Programming 2 08-05-2006 11:50 AM
sed command ancys Programming 5 08-04-2006 12:39 AM
sed command rharris72 Programming 6 11-20-2005 09:26 PM


All times are GMT -5. The time now is 10:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration