How to use SED to join multiple lines?

Hi guys,

anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line?
The end of each line will be replaced with a special char "#".

I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines are joined together.

sed 'N;N;s/\n/#/g'

Example of Input File:

Builtin :Sign CA
"CN=CA,OU=Root CA,O=Sign nv-sa,C=BE"
"CN=CA,OU=Root CA,O=Sign nv-sa,C=BE"
04:00:00:00:5A:C3:94
3E:09:51:92:E1:B7:5D:37:9F:B1:87:29:8A
rsa
2048
Sep 1, 1990 8:00:00 PM
Jan 30, 2020 8:00:00 PM
false
true
true
false
true
true
false
false

TIA

Hi.

I think this is what you mean. If not, please post how your output should look.

sed -e :a -e ';$!N;s/\n/#/;ta' input_file > output_file

Or something similar in awk:

awk -v ORS="#" 1 input_file > output_file

Or with paste

paste -sd"#" input_file > output_file

Or with tr

tr "\n" "#" < input_file > output_file

As you can see, all easier than sed!

Another (one_of_the_with) with perl would be,

perl -e 'while(<>){chomp;print $_,"#";}' infile > outfile

another one in perl :

perl -nle "BEGIN{$\='#'} print " infile > outfile

Hello ,
doesnt work . Can you explain the logic behind it.

gaurav@localhost:~$ echo -e "hello\nHi" |perl -nle 'BEGIN{$\='#'} print'
syntax error at -e line 1, at EOF
BEGIN not safe after errors--compilation aborted at -e line 1.

$\ is a special variable in perl that represents output record separator.


echo -e "hello\nHi" |perl -nle "BEGIN{$\='#'} print  "

  1. make sure you give a space between the print and double quote which you havent done in your statement

  2. try the perl comamnd in double quotes instead of single quote

Thanks
Penchal

i think the following serves the purpose:
sed 's/\n/#/g' source_file_name >destn_file_name .
Please correct me if i am wrong.

Hello
yes I understood.
Thanks . :slight_smile:

OK. You are wrong.

no problem....

Still not easy, but a tad less complicated:

sed ':a;N;s/\n/#/;ta' infile

Thanks for correcting me on that Bro.
Another representation of your code that works is
sed -e :a -e N -e 's/\n/#/' -e ta original_file_name >new_filename
Please correct if i am wrong.

sed '
H        # append to hold
$!d      # if not last line, delete and start over
g        # last line, copy from hold
s/\n/#/g # replace new line with #
s/^.//   # remove the first spurious new line
' infile