Skip to content

Using AWK – print only phone numbers out of unstructured, unreliably columnised lines

December 15, 2008

Sorry about the g33ky post, but i was looking for an example of how to use awk for this circumstance.  There’s probably another text-manipulation cli app to do the job, but i just wanted to use awk for this one.

Note: Solaris awk is known to be buggy. us the xpg4 version on all solaris boxes, or the “match” function wont work.

1 line of data set example:

20081210 133924638-0500 mmsrb owmmsrelay 193 46 46 Info;MMSRelayTxnLog(220/101)193:46:lc=Op ct=owmmsrelay op=MMBox_view.REQ ms=4.63 uid=114944517818131292 size=0 SrvcID=MMS AppID=”Openwave MMS Relay” AppHost=10.33.233.131 AppPort=8088 MsgID=AElADNwAEADBAC4KIe6D OrigIF=WEB RcptIF=WEB OrigAddr=”+19046219845/TYPE=PLMN” RcptAddr=”+19056219845/TYPE=PLMN” NumRcpts=1 EvTime=20081210133924 EvStatus=Success Folder=INBOX TrackID=AElADNwAEADBAC4KIe6D PeerHost=10.33.238.131 PeerPort=55476


bash-3.00$ more program.awk

/usr/xpg4/bin/awk ‘

RS = “\n”

{

where=match($0,”\+”)

if (where)

print substr ($0,where+1,11)

}’ test.txt

Alternatively

/usr/xpg4/bin/awk ‘

RS = “\n”

{

where=match($0,”\+”)

if (where)

print substr ($0,where+1,11)

}’

and

# cat test.txt | program.awk

Explanation:

/usr/xpg4/bin/awk ‘ <— calls the correct awk

RS = “\n” <—- record seperator becomes the end of line

{

where=match($0,“\+”) <—– match the first record with REGULAR EXPRESSION , in this case a plus sign.

if (where)

print substr ($0,where+1,11) <—– print out the 11 characters after the found location

}’ test.txt <—- the file name

Output:

bash-3.00$ ./awk.pl

19046219845

Advertisements
One Comment leave one →
  1. September 2, 2009 12:47 am

    This site rocks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: