Solved

awk print only fields that matches an regexp

Posted on 2003-10-22
5
280 Views
Last Modified: 2010-04-22
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
0
Comment
Question by:hegga
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9598614
Hi hegga,

why not use grep ?

Cheers!
Sunny:o)
0
 

Author Comment

by:hegga
ID: 9598730
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
 
LVL 1

Accepted Solution

by:
Asby earned 200 total points
ID: 9599016
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
 

Author Comment

by:hegga
ID: 9599087
Well, this worked perfectly!
I'll reward the points

Thanks!

--
Hegga
0
 
LVL 7

Expert Comment

by:glassd
ID: 9599373
How about something like:

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A short film showing how OnPage and Connectwise integration works.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now