how to combine 2 lines in same files based on any text

hi,

I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only
Here i am writing example.

Ex1:
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz

output should be
line 1: return abcdefghijklmnopqrstuvwxyz

joint based on '&' .
Thanks in Advance

Added some lines as example to show it will not append other lines below:

$> cat infile
abcdefgh&
ijklmnopqr&
stuvw&
xyz
some
more bla
$> sed -e :a -e '/&$/ {N; s/&\n//}; ta' infile
abcdefghijklmnopqrstuvwxyz
some
more bla

Try this,

cat <Filename> | xargs -E '&' | sed 's/& //g'

why is xyz joined together then?

HI ramesh SRK,

Thank you. "cat <Filename> | xargs -E '&' | sed 's/& //g'" this is working. But still some problem... But some problem.
here i ll expalin with example

line 1 : abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz

Ur script output : abcdefghijklmnopqrstuvwxyz.

but
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz
line 5 : lalalala

then output should come
line 1 : abcdefghijklmnopqrstuvwxyz
line 2 : lalala

but evrything is coming in a single line
line 1 : abcdefghijklmnopqrstuvwxyzlalala

please help me ....

Did you ignore my answer? It does exactly what you asked now the 2nd time. :confused: I just forgot to add the word "return" in the first line but it doesn't matter generally and can be added. Result should be the same.

Edit:
Don't want to sound rude, just wondering.

sorry Zaxxon....I am not much familiar with shell scripting. Just learning now. I dont know exact use of "sed" command. I just copied ur code and tried here . :slight_smile:
But error coming "sed: command garbled: /&$/ {N; s/&\n//}; ta"

I couldnt fix this error.

An approach with awk:

awk '/&$/{sub("&$","");printf $0;next}1' file

Ok :wink: For the sed thing you might try to use GNU sed if available on your OS.

hi Zaxxon,

ur script is working only when i give like below format
CASE 1:
sed -e :a -e '/&$/ {
> N
> s/&\n//
> }
> ta' infile
But if i give

CASE 2:
" sed -e :a -e '/&$/ {N; s/&\n//}; ta' infile"
can u tel me what was the problem for above line?
I tried different different cases...

CASE 3:
"sed -e :a -e '/&$/ { N; s/&\n//;}; ta' infile"
above line gives output only first iteration
that means it checks '&' only once.

$> cat infile
abcdefgh&
ijklmnopqr&
stuvw&
xyz
some
more bla

OUTPUT coming like this
abcdefghijklmnopqr&
stuvwxyz
some
more bla

NOTE : Im using solaris...gsed command not there..

thanks in advance.......

At first:
Please use CODE-tags when you display code, data or logs. It really enchances readability, formatting and preserves special characters. It could happen that you might get a warning or a charge on bits of your forum account if you proceed like that :wink:

Hm...
:a is a label and as long as the condition is true ie. the last character in the line is a & it should start over at the label :a. :ta just says "if-true-go-to-a.

I am not sure but I have something similar on AIX where I have only a plain sed. It seems it is expecting the commands inside the curled bracket one per line. Here the problem I get like you got:

> sed -e :a -e '/&$/ {N; s/&\n//}; ta' infile
sed: 0602-404 Function /&$/ {N; s/&\n//}; ta cannot be parsed.

Here changed so it is working:

$> sed -e :a -e '/&$/ {
> N
> s/&\n//
> }
> ta' infile
abcdefghijklmnopqrstuvwxyz
some
more bla
undef $/;
$str=<DATA>;
my @arr=split(/(?:[^&])\n/,$str);
map {s/&[^:]*:\s*//g} @arr;
print join "\n", @arr;
__DATA__
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz