Operating System - HP-UX
1829148 Members
2197 Online
109986 Solutions
New Discussion

Joining Lines after pattern search

 
Chris Frangandonis
Regular Advisor

Joining Lines after pattern search

Hi All,

Is there a way to join several lines every time a pattern is found for e.g

(Disp is a tool to convert binary in a readable format for e.g the output would look like

DSB=fb 00 80 00 01 0a 011616527100 0000 0000 04081c141833 0a 011450225000000000
+++= 00000010001c0000002d20000101ac024000ad0414001400064a4744484142ce
+++= 02aa03041023ab08454e5155495234201702e3172d20000000000000009b7500)

Disp Filename | awk '{if($1~/^Chris/){
hexstr="0123456789abcdef"
newnum=tolower(substr($8,3,2) substr($8,1,2))
MONT=tolower(substr($10,3,2))
....
....
}
}
if($1~/^DSB/){
#--> here I get stuck trying to add "+++=" to every /DSB/ found, because it belongs in the same line. I can use the following statement in a different awk patterns, but certain files contain both Chris and DSB and some only DSB.

awk '/DSB=/{printf "%s%s",sep,$0;sep="\n";next}
{printf "%s",$0}
END{print}' | awk '/DSB/{

hexstr="0123456789abcdef"
newnum=tolower(substr($4,3,2) substr($4,1,2))
MONT=tolower(substr($10,3,2))
....
....
}
}'

Many Thanks
Chris
6 REPLIES 6
Kent Ostby
Honored Contributor

Re: Joining Lines after pattern search

Chris -- Could you post a snippet of input and a snippet of output. I think I can write you up something in awk but I don't understand the before / after look.

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Rodney Hills
Honored Contributor

Re: Joining Lines after pattern search

How about a small perl one-liner-

Disp Filename | perl -ne 'chomp; print $nl if /^DSB/; print $_; $nl="\n";' -e 'print "\n";'

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Joining Lines after pattern search

Oops, one change-

Disp Filename | perl -ne 'chomp; print $nl if /^DSB/; print $_; $nl="\n";' -e 'END{print "END\n";}'

-- Rod Hills
There be dragons...
Chris Frangandonis
Regular Advisor

Re: Joining Lines after pattern search

Hi Kent,

Thanks for the reply.

The before snippet is

1)DSB=fb 00 80 00 01 0a 011616527100 0000 0000 04081c141833 0a 011450225000000000

2)+++=00000010001c0000002d20000101ac024000ad0414001400064a4744484142ce
3)+++=02aa03041023ab08454e5155495234201702e3172d20000000000000009b7500
4)DSB=fb 00 80 00 01 0a 039254023500 0000 d801 04081c141834 0a 031462586600000000
5)+++=0000001000180000001720000102ac026000ad041b001a000644544d484142b9
6)
+++=000b

This is were I have a problem in joining these lines. What it should look like before I define (newnum=tolower(substr($4,3,2) substr($4,1,2))

Pionts 1 to 3 is a record for e.g
DSB=.. .. .. .. .. +++=.. .. .. ... .. .. +++= .... .. .. .. .. .. .. ..
DSB=.. .. .. .. .. +++=.. .. .. .. +++=.. .. ... is the next record.

Please note that I first search for /Chris/ and then define etc but when the next record starts with ^DSB I use the /DSB/ and define ( as shown in my previous post)so that my output could look like plain simple text format for both Chris and DSB for e.g.

And the result whould be

0116165271 2004-08-28 20:24:51 0114502250 0

Many Thanks
Chris
Kent Ostby
Honored Contributor

Re: Joining Lines after pattern search

Chris --

I'm still not sure what you want to do WITH the data, but this script will combine the lines for you and then you can put your magic in the two spots where I have "Do Stuff" listed.

BEGIN {started=0;}
/^DBS/ {if (started > 0 )
{Do stuff };
started=1; keptline=$0;
next;}
{keptline=keptline $0;}
END {Do stuff}
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Chris Frangandonis
Regular Advisor

Re: Joining Lines after pattern search

Hi Kent,

Thanks

Part of my Script looks like this

$RAW $2 |awk '{
if($1~/Chris/){
for (e=1;e LA=$(e+4)
{if ($(e+4)==00){
Anum="NULL"
}else{
A_num=$(e+5)
}}}
if($e~/P=65/){
BNUM=$(e+2)
LN=$(e+1)
}
if($e~/P=64/){
DATE=$(e+1)
DUR=$(e+3)
}
if($e~/P=67/){
UN=$(e+1)
}}
hexstr="0123456789abcdef"
newnum=tolower(substr(DUR,5,2) substr(DUR,3,2) substr(DUR,1,2))
YEAR=tolower(substr(DATE,1,2))
MONT=tolower(substr(DATE,3,2))
DAY=tolower(substr(DATE,5,2))
HOUR=tolower(substr(DATE,7,2))
................
printf ("%15s %.2d-%.2d-%.2d %.2d:%......
}
}
if($1~/DSB=/){
hexstr="0123456789abcdef"
newnum=tolower(substr($9,3,2) substr($9,1,2))
YEAR=tolower(substr($10,1,2))
MONT=tolower(substr($10,3,2))
DAY=tolower(substr($10,5,2))
HOUR=tolower(substr($10,7,2))
MIN=tolower(substr($10,9,2))
...........................
printf ("%11s %.2d-%.2d-%.2d %.2d:%.2d:%.2
}
}'

Please note that Chris format differs to the DSB one and yes to your answer, after the +++= there is additional info that I require.

How could I use your script in this case????


Thanks Once Agaqin
Chris