LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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

Reply
 
LinkBack Search this Thread
Old 05-04-2009, 09:31 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
Question sed help - replace line feed with different character


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!
 
Old 05-04-2009, 10:41 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,002
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
Old 05-04-2009, 10:48 AM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: solihull.w-mids.uk
Distribution: Debian 5.0, CentOs, Solaris 8-10
Posts: 573

Rep: Reputation: 58
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
 
Old 05-04-2009, 10:59 AM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Independance, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 160Reputation: 160
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/
 
Old 05-04-2009, 11:31 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 293

Rep: Reputation: 112Reputation: 112
Another method
Code:
echo 'abc
def
ghi' | paste -sd';'
abc;def;ghi
 
Old 05-04-2009, 12:40 PM   #6
bradvan
Member
 
Registered: Mar 2009
Posts: 82

Original Poster
Rep: Reputation: 16
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!
 
  


Reply

Tags
awk, perl, regex, sed, tr


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing new line character using sed Fond_of_Opensource Linux - Newbie 4 03-26-2010 03:19 PM
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
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 04:46 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration