Solved

awk print only fields that matches an regexp

Posted on 2003-10-22
5
284 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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