Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Over the past year I have found your site to be EXCELLENT. Never have I been able to find so many answers to such vast problems and it is an excellent service..."

Geography

Where in the world do Tek-Tips members come from?

AWK - print only matching field itself and not lineHelpful Member!(2) 

dls0 (IS/IT--Management)
19 Mar 08 19:28
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
ZaSter (TechnicalUser)
19 Mar 08 19:53

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 -

Helpful Member!  Annihilannic (MIS)
19 Mar 08 20:54
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.

ZaSter (TechnicalUser)
19 Mar 08 21:53

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

- ZaSter -

dls0 (IS/IT--Management)
19 Mar 08 23:10
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
Helpful Member!  PHV (MIS)
20 Mar 08 4:10
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: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

ZaSter (TechnicalUser)
20 Mar 08 5:11

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

- ZaSter -

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close