sed help - replace line feed with different character
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
- 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
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).
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.