LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed replace end of line with character (https://www.linuxquestions.org/questions/programming-9/sed-replace-end-of-line-with-character-4175449966/)

grob115 02-13-2013 05:01 AM

sed replace end of line with character
 
Hi, if I have a file with 10 lines, and all I want is to join the list of lines together in my file into a single line, by the characer '&', can someone explain why I need to do the following.
Code:

sed ':a;N;$!ba;s/\n/\&/g'
And can not with the following.
Code:

sed 's/\n/\&/g'

acid_kewpie 02-13-2013 05:44 AM

because sed is line based, to doesn't see the \n character, as that delimits the lines it processes.

tr would work easily though...
Code:

tr '\n' '&'

druuna 02-13-2013 05:45 AM

Quote:

Originally Posted by grob115 (Post 4890600)
Hi, if I have a file with 10 lines, and all I want is to join the list of lines together in my file into a single line, by the characer '&', can someone explain why I need to do the following.
Code:

sed ':a;N;$!ba;s/\n/\&/g'
And can not with the following.
Code:

sed 's/\n/\&/g'

\n isn't part of the buffer (pattern space) when one line is stored, it is when multiple lines are stored in the buffer.

Have a look here: How to match newlines in sed

BTW: Wouldn't this be a better solution:
Code:

cat infile | tr '\n' '&'

grob115 02-13-2013 01:44 PM

Ah damn. Thanks guys. Knew 'sed' is line based but got lost in within what I was trying to do.

danielbmartin 02-13-2013 03:01 PM

I like to use paste for this transformation.

InFile ...
Code:

albert
bernard
charles
david
edward
frank
george
henry
irving
john

Code ...
Code:

paste -sd\& $InFile > $OutFile
OutFile ...
Code:

albert&bernard&charles&david&edward&frank&george&henry&irving&john
Daniel B. Martin

grail 02-14-2013 09:18 AM

Code:

awk 'ORS="&"' file

danielbmartin 02-14-2013 10:03 AM

Minor nitpick
 
Quote:

Originally Posted by grail (Post 4891561)
Code:

awk 'ORS="&"' file

This awk produces a character string with a trailing ampersand. That's not exactly what the OP asked for.

Daniel B. Martin

danielbmartin 02-14-2013 10:16 AM

Quote:

Originally Posted by acid_kewpie (Post 4890636)
tr would work easily though...
Code:

tr '\n' '&'

This code ...
Code:

echo
echo; echo "Method of LQ Moderator acid_kewpie as posted"
echo; echo "InFile ..."; cat $InFile
tr '\n' '&' $InFile > $OutFile
echo "OutFile ..."; cat $OutFile

echo
echo; echo "Method of LQ Moderator acid_kewpie with <"
echo; echo "InFile ..."; cat $InFile
tr '\n' '&' < $InFile > $OutFile
echo "OutFile ..."; cat $OutFile

... produces this output ...
Code:

Method of LQ Moderator acid_kewpie as posted

InFile ...
albert
bernard
charles
david
edward
frank
george
henry
irving
john
tr: extra operand `/home/daniel/Desktop/LQfiles/dbm630inp.txt'
Try `tr --help' for more information.
OutFile ...


Method of LQ Moderator acid_kewpie with <

InFile ...
albert
bernard
charles
david
edward
frank
george
henry
irving
john
OutFile ...
albert&bernard&charles&david&edward&frank&george&henry&irving&john&

Why is it necessary to use the "<" to avoid the "extra operand" error message?

Daniel B. Martin

colucix 02-14-2013 10:22 AM

Quote:

Originally Posted by danielbmartin (Post 4891593)
Why is it necessary to use the "<" to avoid the "extra operand" error message?

Because tr doesn't accept a file name as argument: it works only on standard input.

danielbmartin 02-14-2013 10:28 AM

Quote:

Originally Posted by druuna (Post 4890637)

I clicked and got a pure gray screen; no text.

Daniel B. Martin

druuna 02-14-2013 11:33 AM

Quote:

Originally Posted by danielbmartin (Post 4891601)
I clicked and got a pure gray screen; no text.

Daniel B. Martin

Works on my side. Checked with Opera and disabled: cookies, plugins and javascript.

Just tried it with Firefox (Iceweasel) and it shows a grey screen....... Try manually adding index.php

grail 02-14-2013 12:03 PM

Quote:

This awk produces a character string with a trailing ampersand. That's not exactly what the OP asked for.
As does the tr solution, I picked up on the idea of replace all newlines with an ampersand as opposed to create a string with ampersand as the separator.

danielbmartin 02-14-2013 12:43 PM

Quote:

Originally Posted by druuna (Post 4891641)
Try manually adding index.php

This did the trick! Thank you.

Daniel B. Martin

mina86 02-14-2013 05:52 PM

Code:

sed -n -e1h -e'2,$'H -e'$x' -e'$s/\n/\&/g' -e'$p'


All times are GMT -5. The time now is 12:46 AM.