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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inlist command within AWK 1

Status
Not open for further replies.

douggy

Technical User
Jan 18, 2001
78
GB
I have a text file which contains a post area field.

For example 'SL' for Slough.

I would like to to print out all the records that have one of the following post areas.

So anything that is either 'RG', 'SL', 'BN' or 'MK'.

How do I write an inlist command in AWK.

Thanks for you help.

Regards
Mark Holloway
 
This depends to a degree on what else is in the file, but at the simplest level you could say:

awk '/RG/ || /SL/ || /BN/ || /MK/ {print}' filename

This will match any occurrence of RG in the record. If you need to limit it to only consider the relevant field then let us know.

Greg.
 

Hi douggy and grega!

Pattern of grega's awk command can be simpler:

awk "/RG|SL|BN|MK/" textfile

You can omit print statement. If {procedure} is missing, the entire matched line is printed. I like this feature of awk programming language.

Bye!

KP.
 

douggy and grega,

I use DOS version of awk. *nix awk command must be with single quotes (instead double quotes).

Bye again!

KP.
 
Krunek,
What version of awk for dos are you using and where did you get it? Is it open source?

Thanks.
MD
 
Hi Grega.

Thanks for your help.

It would be great if you could tell me how to limit this to one relavant field.

Kind regards

Mark
 
Hello marsd!

I use Gawk 3.0.4 for DOS, Win and Linux. This is GNU version of awk (GNU, thanks! God bless you!). GNU software is free, but copyrighted. You can visit this site:


Here you can find excellent Gawk manual (author is Arnold Robbins and some other people).

You can download gawk from this site:


Congratulation, awk is a good choice.

Bye!

KP.
 

douggy,

If the relevant field is, for example, 4th field of input line, awk program for your purpose can be:

awk '$4 == "RG" || $4 == "SL" || $4 == "BN" || $4 == "MK"' textfile

Bye!

KP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top