Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining Lines with Pattern 1

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

Is there a way to join several lines every time a pattern is found.
I know this can be done by using the following
awk '/DSB=/{printf "%s%s",sep,$0;sep="\n";next}
{printf "%s",$0}
END{print}'


But what I am looking for is when /DSB/ is found the next line starts ^+++=. I need these lines to be joined until the next /DSB/ without a print statement, then declare e.g

YEAR=tolower(substr($2,1,2))
MONT=tolower(substr($2,3,2))
.... etc

My input

DSB=fb 00 80 00 01 0a 011616527100 0000 0000 04081c141833 0a 011450225000000000
+++= 00000010001c0000002d20000101ac024000ad0414001400064a4744484142ce
+++= 02aa03041023ab08454e5155495234201702e3172d20000000000000009b7500
DSB=fb 00 80 00 01 0a 039254023500 0000 d801 04081c141834 0a 031462586600000000
+++= 0000001000180000001720000102ac026000ad041b001a000644544d484142b9
+++= 000b

And Part of script

Disp Filename | awk '{if($1~/^DSB/){
# this is where I get stuck
hexstr="0123456789abcdef"
newnum=tolower(substr($4,3,2) substr($4,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))

(Disp is a tool to read in hex format)

Many Thanks
Chris
 
# this is where I get stuck
Disp Filename | awk '
function hex2dec(h ,x,v){
h=tolower(h)
for(i=1;i<=length(h);++i){
x=index("0123456789abcdef",substr(h,i,1))
if(!x)return"NaN"
v=(16*v)+x-1
}
return v
}
/^DSB/{
newnum=hex2dec(substr($4,3,2) substr($4,1,2))
YEAR=hex2dec(substr($10,1,2))
MONT=hex2dec(substr($10,3,2))
DAY=hex2dec(substr($10,5,2))
HOUR=hex2dec(substr($10,7,2))
# ...
}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
HI PHV,

Many Thanks for that function statement, will definetly come in hand. The output from your posting looks like this :

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

but where is the the additional lines that must be joined for e.g
1)DSB=fb ... .. .. .. .. .. ..+++= .. ..... .. ... +++=
2)DSB=fb .. .. .. .. .. ...+++= .. . .. .. .. +++= .. ..


Once line line is created, ~/DSB/ and then define

Many Thanks
Chris








 
Brute force method:
Disp Filename | awk '
/DSB=/{printf "%s%s",sep,$0;sep="\n";next}
{printf "%s",$0}
END{print}
' | awk '
function hex2dec(h ,x,v){
h=tolower(h)
for(i=1;i<=length(h);++i){
x=index("0123456789abcdef",substr(h,i,1))
if(!x)return"NaN"
v=(16*v)+x-1
}
return v
}
/^DSB/{
newnum=hex2dec(substr($4,3,2) substr($4,1,2))
YEAR=hex2dec(substr($10,1,2))
MONT=hex2dec(substr($10,3,2))
DAY=hex2dec(substr($10,5,2))
HOUR=hex2dec(substr($10,7,2))
# ...
}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top