LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , , , ,

Reply
 
Thread Tools
Old 05-04-2009, 09:31 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 45
Thanked: 0
Question sed help - replace line feed with different character


[Log in to get rid of this advertisement]
I'm trying to take the output of a command which is listed on many lines (for example: rpm -qa | grep xorg) and change it to one line of output and replace the line feeds with some other character. I have:

Code:
sed -e '{
N
s/\n/;/
}'
which partially works, but it only catches every other line. Does anyone have a suggestion?

Thanks!
bradvan is offline  
Tag This Post , , , , ,
Reply With Quote
Old 05-04-2009, 10:41 AM   #2
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
Hi,

2 things:

- Can this be done with sed: Yes -> sed ':a;N;$!ba;s/\n/;/g' infile. I do believe (not my own sed oneliner) that this would involve using sed's buffer space, I'm not sure how big that space is. It could become an issue.

- Is there a better/other way: Yes -> tr '\n' ';' < infile

Hope this helps.
druuna is offline  
Tag This Post ,
Reply With Quote
Thanked by:
Old 05-04-2009, 10:48 AM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: solihull.w-mids.uk
Distribution: Debian 5.0, CentOs
Posts: 499
Thanked: 41
It's quite easy in Perl. Just pipe the output to:
Code:
perl -e 'while(<>){chomp;print "$_;";}'
This reads each line from STDIN - while(<>), strips the newline - chomp and prints the stripped line followed by a semi-colon - print "$_;" ($_ is a Perl default variable).

QED
Robhogg is offline  
Tag This Post
Reply With Quote
Thanked by:
Old 05-04-2009, 10:59 AM   #4
rweaver
Member
 
Registered: Dec 2008
Location: Independance, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 548
Thanked: 61
Quote:
Originally Posted by bradvan View Post
I'm trying to take the output of a command which is listed on many lines (for example: rpm -qa | grep xorg) and change it to one line of output and replace the line feeds with some other character. I have:

Code:
sed -e '{
N
s/\n/;/
}'
which partially works, but it only catches every other line. Does anyone have a suggestion?

Thanks!
sed -n 'H;${g;s/\n//g;p}' filename

is the only way I know to do it using sed, but there are easier ways using tr and perl. Check it out, found this thread on LQ...

https://www.linuxquestions.org/quest...ewline-530376/
rweaver is online now  
Tag This Post
Reply With Quote
Thanked by:
Old 05-04-2009, 11:31 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 171
Thanked: 32
Another method
Code:
echo 'abc
def
ghi' | paste -sd';'
abc;def;ghi
Kenhelm is offline     Reply With Quote
Thanked by:
Old 05-04-2009, 12:40 PM   #6
bradvan
Member
 
Registered: Mar 2009
Posts: 45
Thanked: 0

Original Poster
Thanks for the replies! I had tried the tr, but I must have messed up the syntax. What you supplied worked. The sed doesn't want to work on one line. It reports label too long. If I break it up so that :a is on it's own line, then it works. The perl worked also as well as the paste command (adding - to the end). Give a task to a group of programmers and most often you will get unique answers that accomplish that task.

Thanks to all!
bradvan is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Replace 2nd to last Character with SED elproducto Programming 5 03-31-2009 01:41 PM
how to search and replace character from middle of line vishal_titre Linux - General 7 09-30-2008 03:46 AM
removing new line character using sed Fond_of_Opensource Linux - Newbie 3 11-21-2006 03:57 AM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 03:27 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 03:23 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration