Shell script to join a line to the line that follows it

Hi All,

I need a shell script to join a line to the line that follows it. But I shouldn't do it for all the lines. I need to join a line having the character say '&' at the end of the line & need to join the line that follows it.

E.g

Input

  1. This is the first line &
  2. and the second line
  3. third line
  4. fourth line

Output

  1. This is the first line and the second line
  2. third line
  3. fourth line

Thanks
Shashi

try awk:

awk '{ if(index($0,"&")==length($0) ){
           sub(/&/,"",$0)
           printf("%s ", $0 )
       }
       else {
           print $0
       } }     ' filename 

filename before

This is the first line &
and the second line
third line &
fourth line
line 5
line 6 &
line 7
line 8

results:

This is the first line  and the second line
third line  fourth line
line 5
line 6  line 7
line 8 

The following concatenates lines with a trailing \
which is the usual requirement:

sed ':a; /\\$/N; s/\\\n//; ta'

So assuming your prepended numbers are only for illustration,
you want:

sed ':a; /&$/N; s/&\n//; ta'

If you do need prepened numbers then I guess you could
do something like:

cut -d. -f2- t |
sed ':a; /&$/N; s/&\n//; ta' |
cat -n

Hi,

Thanks for ur reply. But I am still facing problems. Could u please help me with using a file name.

Thanks
Shashi

I tried using both the ways but still no success. I might be doing in a wrong way

awk '{if ( $0 ~ /&$/ ) { printf "%s ",$0 } else { print $0 }'} filename|sed "s/&//"

$ cat filename
This is the first line &
and the second line
third line &
fourth line
line 5
line 6 &
line 7
line 8

$ awk 'sub(/&$/,""){printf $0;next};1' filename
This is the first line and the second line
third line fourth line
line 5
line 6 line 7
line 8