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!

Find pattern print, then grab either the next 2 or 3 lines 2

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

Can anyone please assist me with my awk statement.
I am trying to search for a pattern in $7 field and if true print ,read next line and if the next line starts with +++= print, read the next line and if next line starts with +++= print and if not, return back to the search criteria and so on.

awk '{if($7~/0152670293/){
print
if (!(getline > 0)) exit
while (NF > 1 && $1 = "+++=") {
print
if (!(getline > 0))exit
}
}
}' file

By running the above scripts my output
DSB=fb 00 80 00 04 0a 015267029300 0000 0000 040907141839 0a 013799194700000000
+++= 0000001000290000004620090101ac02c000ad043f003d00064a4744484142f9
+++= 00aa03041023ab08454e5155495259201704d907cc1fe007462000000005000a
+++= 0000001000290000004620090101ac02c000ad043f003d00064a4744484142f9
+++= 00aa03041023ab08454e5155495259201704d907cc1fe007462000000005000a
etc


Input File
DSB=fb 00 80 00 01 0a 011883633600 0000 0000 04090714192b 0a 012314891100000000
+++= 0000001000110000007f20000101ac024000ad042d002c00064a47444841428e
+++= 03aa03041023ab08454e515549523220170239087f200000000000000035000e
DSB=fb 00 80 00 04 0a 015267029300 0000 0000 040907141839 0a 013799194700000000
+++= 0000001000290000004620090101ac02c000ad043f003d00064a4744484142f9
+++= 00aa03041023ab08454e5155495259201704d907cc1fe007462000000005000a
DSB=fb 00 90 00 04 0a 027213491800 0000 0000 04090714192b 00 000000000000000000
+++= 3000000001020000002218090101064a4744484142a703aa03041023ab08454e
+++= 5155495259200084f100f27cf100f28801a0bc50009f7900009f790100000000
DSB=fb 00 80 00 04 0a 043743718300 0000 5600 04090714192c 0a 043726332600000000
+++= 210000000035000000581a090102130553555a414e03094d414b484142414e45
+++= 064a
+++= 474448414200060b504c5a4841427600aa03041025ab0844454d414e44202000c8d893000000000080e71c13a0be3a0000016800000005f680e9b04351d3
DSB=fb 00 80 00 01 0a 039978312700 0000 0000 04090714192d 0a 021671859400000000
+++= 0000001000400000006920000101ac024000ad0414001400064a474448414225
+++= 06aa03041023ab08454e51554952592017027f17692000000000000000000000

Many Thanks
Chris
 
Hi All,

I think I found a temp solution

awk '{if($7~/0114357894/){
print
flg=1;next
}
{if($1~/^\+++/ && flg==1){
print;next
}else{
flg=0
}
}
{if($1~/^\+++/ && flg==1){
print;next
}else{
flg=0
}
}
{if($1~/^\+++/ && flg==1){
print;next
}else{
flg=0
}
}
}' file

I would like see an improved version to this one.

Again Thanks
Chris
 

Code:
more { if ($0 ~ /^\+\+\+=  *[^ ]/) {print; next} else more=0 }
$7 ~ /0152670293/ { print; more=1 }

 
Hi Futurelet,

Thanks. Thats great.
If I may add on to my question, how can I add this in a function like so

function get_b_name(file)
{

TOOL="/usr/bin/which toolDB 2>/dev/null"
TOOL | getline TOOL2;close(TOOL)
while(((TOOL2" "FILE)|getline)>0)
{if($7~/0152670293/){print;flg==1;getline}}}
{if($1~/^\+++/ && flg==1){print;getline}else{flg=0}}}}
{if($1~/^\+++/ && flg==1){print;getline}else{flg=0}}
{if($1~/^\+++/ && flg==1){print;getline}else{flg=0}}
close(TOOL2" "FILE)

and from the above function I only get the first line. What am I doing wrong???

Result
DSB=fb 00 80 00 04 0a 015267029300 0000 0000 040907141839 0a 013799194700000000

Thanks
Chris
 
Does this do what you want?
Code:
$7 ~ /^0152670293/ {
    flag = 1
    print
    next
}
/^+++=/ {if (flag == 1) print}
!/^+++=/ {flag = 0}
Output with the sample you posted:
Code:
DSB=fb 00 80 00 04 0a 015267029300 0000 0000 040907141839 0a 013799194700000000
+++=   0000001000290000004620090101ac02c000ad043f003d00064a4744484142f9
+++=   00aa03041023ab08454e5155495259201704d907cc1fe007462000000005000a


 
As a function; pass it the filename, e.g., somefunc(filename)
Code:
function somefunc(file,    flag) {
    while (getline <file) {
        if ($7 ~ /^0152670293/) {
            flag = 1
            print
        } else {
            if (/^+++=/) {
                if (flag == 1) {
                    print
                }
            } else {
                flag = 0
            }
        }
    }
    return 0
}
 
Hi Mikevh,

Many Thanks (Great stuff and Great Forum). It works wonders.

Once Again Thanks
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top