Remove new line starting with a numeric value and append it to the previous line

Hi,

i have a file with multiple entries. After some tests with sed i managed to get the file output as follows:

lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1,
00,2110597,2094790,0,81,529,75649011,56435363,
lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,
00,1019687575,1327955853,1,322,583,2810019516,1981897836,
lsn=TM1ITP1-AM1ITP2-LS,apc=501:0,state=avail,avail/links=1/1,
00,536022841,1334912507,0,277,966,2588691785,2512216939,
lsn=TM1ITP1-TMNLSTP1,apc=12309:1,state=avail,avail/links=4/4,
00,28422611,35850253,0,185,216,216892933,38975423,
01,66892965,50298069,0,250,8807,2591394100,803622868,
02,27488395,38094791,0,249,252,170335784,66760929,
03,27762094,19995809,0,251,258,216422353,2019547890,
lsn=TM1ITP1-TMNLSTP2,apc=12547:1,state=avail,avail/links=4/4,
00,80966204,16449140,0,47,121,1928336154,2818020786,
01,332655,171104,0,50,30,25432178,25498178,
02,55281799,11176343,0,93,145,4243472214,1907308254,
03,54060754,28585708,0,363,301,4174500371,4240529245,
lsn=X-LINK-IN1,apc=4882:1,state=avail,avail/links=1/1,
00,40085787,31652380,0,73,755,271023232,3802680330,

What i'm trying to perfrom now is to be able to shift any line starting with a number (in this case they all start with 0) to be shifted on to the next line. or maybe more simpler, all lines which do not begin with "lsn=" are to be appened to the previous line
Example:

lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1,00,2110597,2094790,0,81,529,75649011,56435363,
....
lsn=TM1ITP1-TMNLSTP2,apc=12547:1,state=avail,avail/links=4/4,00,80966204,16449140,0,47,121,1928336154,2818020786,01,332655,171104,0,50,30,25432178,25498178,02,55281799,11176343,0,93,145,4243472214,1907308254,03,54060754,28585708,0,363,301,4174500371,4240529245,

Is there a way with sed or tr?

---------- Post updated at 02:47 PM ---------- Previous update was at 02:30 PM ----------

ok it seems this does the trick!

awk 'BEGIN {accum_line = "";} /^lsn=/{if(length(accum_line)){print accum_line; accum_line = "";}} {accum_line = accum_line " " $0;} END {if(length(accum_line)){print accum_line; }}'

Good you found a solution on your own. You might want to consider to put the preliminary sed stuff into that final awk script as well.

In the sample output you said you were trying to produce, there weren't any leading <space> characters at the start of your output lines and there weren't any <space> characters in the line where lines were joined. So, why does your script use:

{accum_line = accum_line " " $0;}

instead of just:

{accum_line = accum_line $0;}

?

Another idea

awk 'length{gsub(/\n/, ""); print RS $0 }' RS='lsn=' infile

This will work if you're using GNU awk .

But, according to the standards, the results are unspecified if the value assigned to RS is more than one character. On most BSD- and UNIX-systems, awk only looks at the first character of RS and ignores any remaining characters in that variable.

1 Like

Hi Don, you are correct.

in fact, I'm using the $0 now and it's working.

Thanks for the info.