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

AWK - print only matching field itself and not line 2

Status
Not open for further replies.

dls0

IS-IT--Management
Mar 19, 2008
2
0
0
Hello to all,

I am new at learning awk and want to make sure that the kind of problem I face can be solved within awk so I am not wasting my time.

My problem is conceptually simple:

I want to match a string using a regex pattern and have awk print out only the field(s) that matches the pattern and not the entire line.

Here is the hard part for me.
I will not know ahead of time exactly which field $1 - $NF it is going to match and the field within the line that matches can vary from file to file so, I need awk "to know" that it doesn't matter what field within the line that matches but print only the fields that match and not the entire line on which the matching field occurs.But I can't specify before hand which field(s) to print because it will vary.

I need to extract specific field values over several thousand files and I hope that awk would let me write the script in a way where I don't have to know ahead of time which field on which line will match.

I know awk can easily match fields and print out the line that matches. And I know that awk can easily print out just those fields that one specifies when a match occurs.

I need awk to allow the field location to vary and know just give me the field when they match the regex and not the entire line.

Something like this.

Match regex pattern P in any field on any line it occurs in file_1 through file_n.
Print only the field value(s) that matches the regex pattern P.

I hope I was clear. I don't expect anyone to show me how to do this but if anyone would be so kind just to answer whether this is indeed something awk can do I would be relieved to know I am not wasting my time as I move forward trying to learn awk to solve this kind of problem.

Thanks,
dls0
 

dls0,

What kind of OS? If it is Linux, or if you have the GNU version of 'grep', then you might want to look at using grep -o which prints only the matched regex and not the surrounding text.

- ZaSter -

 
GNU grep -o would be perfect, but since we're talking about awk here:

Code:
 awk 'match($0,/regexp/) {print substr($0,RSTART,RLENGTH)}' inputfile

The RSTART and RLENGTH variables are the key. Note that additional logic would be required for multiple matches on a line.

Annihilannic.
 

Very nice, Annihilannic! Good to know. Have a star.

- ZaSter -

 
ZaSter and Annihilannic,

Thank you so much I really appreciate the help.

The OS is CENT OS. So the grep -o would probably work. But I do want to become very proficient with awk.

Thanks A Million,
dls0
 
For multiple matches on a line:
Code:
awk '/regexp/{for(i=1;i<=NF;++i)if($i~/regexp/)print $i}' /path/to/inputfile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Thanks, PHV, a purple star for filling in the missing piece.

- ZaSter -

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top