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 '{ Thanks! |
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. |
It's quite easy in Perl. Just pipe the output to:
Code:
perl -e 'while(<>){chomp;print "$_;";}' QED ;) |
Quote:
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/ |
Another method
Code:
echo 'abc |
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! |
Another end line removal
If test.txt is:
first second third the following works for in a BASH (at least) script, so long as LIN doesn't exhaust ENVIRONMENT reserve. #!/bin/bash LIN=$(cat test.txt) echo $LIN > lintest.txt # end cat lintest.txt should show: first second third |
Quote:
First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting. Next, this doesn't replace newlines with semicolons, as the (now 3-year-old) OP wanted. But admittedly it does remove them. And the reason this "works" like that is because of the 'echo $LIN' line. Since the variable isn't quoted, the contents of the variable are word-split after expansion, and echo sees each word in the file as a separate value to print. The problem with using this is that the shell splits values on all contiguous instances of whitespace, not just newlines. It also has an added danger that globbing patterns are expanded as well. Code:
#line 4 is a blank line You could manually disable globbing and set IFS to newline only to work around most of these issues, but why bother when there are better ways to handle it, as documented above. Incidentally, here's another method I just thought of that does replace newlines with semicolons, using bash v4's mapfile command and an array. Code:
$ shopt -s extquote #you may need to enable this first, esp. in scripts Code:
$ text=$( <file.txt ) |
All times are GMT -5. The time now is 02:37 PM. |