LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed help - replace line feed with different character (https://www.linuxquestions.org/questions/programming-9/sed-help-replace-line-feed-with-different-character-723548/)

bradvan 05-04-2009 08:31 AM

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!

druuna 05-04-2009 09:41 AM

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.

Robhogg 05-04-2009 09:48 AM

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 ;)

rweaver 05-04-2009 09:59 AM

Quote:

Originally Posted by bradvan (Post 3529514)
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/

Kenhelm 05-04-2009 10:31 AM

Another method
Code:

echo 'abc
def
ghi' | paste -sd';'
abc;def;ghi


bradvan 05-04-2009 11:40 AM

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!

marvs 04-22-2012 06:15 PM

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

David the H. 04-22-2012 11:31 PM

Quote:

Originally Posted by marvs (Post 4660198)
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


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
$ cat file.txt
line with single spaces
line  with  multiple  spaces
line            with            tabs

line with glob *

$ text=$( <file.txt )
$ echo $text
line with single spaces line with multiple spaces line with tabs line with glob file.txt file2 file3

$ echo "$text"
line with single spaces
line  with  multiple  spaces
line            with            tabs

line with glob *

Quoting the variable preserves all whitespace and other special characters.
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

$ mapfile array <file.txt

$ echo "${array[*]//$'\n'/;}"
line with single spaces; line  with  multiple  spaces; line          with            tabs; ; line with glob *;

Edit: Actually even a single variable would work; and maybe even better (I see there's an extra space inserted between the "lines" in the above, due to the default IFS setting. :doh: Set IFS to null first to fix that.):

Code:

$ text=$( <file.txt )

$ echo "${text//$'\n'/;}"
line with single spaces;line  with  multiple  spaces;line            with            tabs;;line with glob *



All times are GMT -5. The time now is 02:37 PM.