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!

Processing a list 1

Status
Not open for further replies.

zaxxon

MIS
Dec 12, 2001
226
DE
Hello,

I have a list like this:

Code:
A:
   5432112 awdawd
   89672235 egwegs
   33597 sesefs
B:
   8923746 efsqwf
   33333 uh23dd
   23666 uahwd3
C:
   76432 afug23d
   91222 iohiu2
   237215 ih232893
D:
   # and so on

I want to know which label contains the pattern "33333", ie. I would like to have printed "B:".

I already tried following with awk:
Code:
    awk '
    /^[A-Z]:/
    {
                      label = $1
                      while ( NR != 1 )
                      {
                           if ( $1 == "33333" )
                           {
                                 print label
                                 break
                           }
                           else
                           {
                                 getline
                           }
                      }
    }

It doesn't work..
Thanks for a hint/help in forward. If anyone has a simple solution in awk and or sed I would be very thankful.

Bye
Zaxxon

laters
zaxxon
 
Exactly this is covered in full in the O'Reilly Sed and Awk book (which any serious sysadmin ought to own). The answer, and I hope I'm not breaking copyright, is
Code:
#!/bin/ksh
search=$1 # 33333 in your case
awk 'BEGIN { FS= "\n"; RS = ""}
$0 ~ /'"$search"'/ {print $0}' $2
Where the call to the script is
Code:
awkscript searchpattern file

Ceci n'est pas une signature
Columb Healy
 
Hi

Code:
awk '/^[A-Z]:/{l=$0}/33333/{print l}' /input/file

[gray]# or[/gray]

sed -n '/^[A-Z]:/h;/33333/{g;p}' /input/file
Tested with [tt]gawk[/tt] and GNU [tt]sed[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top