awk print only fields that matches an regexp

I have a txt file with som mail adressess that are collected out of my maildir,
now I want awk to print all the mail adressess that matches a regexp.
There is no logical system in the file, so awk will have to check every word.

The file looks something like this:
<%mailadress%>
> an djfklds sdkfjdf %mailadress% dfklødf
to: "jherwkejrh" <%mailadress%>

But there is no system in the since it's been awk'ed out of
my maildir.
Can someone help me with this ?

--
Hegga
heggaAsked:
Who is Participating?
 
AsbyCommented:
Well, I can do it in perl for you. My awk knowledge isn't that good:

cat file | perl -ne 's/^.*\b(\w+\@\w+\.\w+)\b.*$/$1/g,print if /\w+\@\w+\.\w+/'
0
 
sunnycoderCommented:
Hi hegga,

why not use grep ?

Cheers!
Sunny:o)
0
 
heggaAuthor Commented:
Because grep returns the whole line where
the regexp matched. I dont want the hole line,
just the field that matches the regexp.

--
Hegga
0
 
heggaAuthor Commented:
Well, this worked perfectly!
I'll reward the points

Thanks!

--
Hegga
0
 
glassdCommented:
How about something like:

awk '{
   for(i=1;i<=NF;i++) {
      if($i ~ /^%..*%$) {
         print $i
      }
   }
{' infile
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.