Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AWK - print only matching field itself and not line

AWK - print only matching field itself and not line

AWK - print only matching field itself and not line

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.


RE: AWK - print only matching field itself and not line


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 -

RE: AWK - print only matching field itself and not line

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


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.


RE: AWK - print only matching field itself and not line

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

- ZaSter -

RE: AWK - print only matching field itself and not line

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,

RE: AWK - print only matching field itself and not line

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

- ZaSter -

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login

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