LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-06-2012, 02:25 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Rep: Reputation: 57
AWK Restrictive Search


I have a file with most of the lines formatted in this way:

Code:
testaccount:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5
There are a few lines with:

Code:
testaccount2:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:reminder2012 $56.00

and 
testaccount2:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:$56.00
using AWK I have a one-liner that excludes case sensitivity, uses the field delimeter ":" and searches for the account name and if there is any ip adress/ip adresses associated with the string:

Code:
cat wspasswd| awk -F ':' 'tolower($1) ~ /^testaccount2/ && /[0-9][0-9][0-9][0-9]/'
which print the whole sting correctly:

Code:
testaccount2:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:$56.00
That is fine but my question is, how can I have awk additionally, once it finds the account name and the ip adress/ip adresses to print out "only the ip adress/ip adresses"

using testaccount2 as an example

Code:
cat wspasswd| awk -F ':' 'tolower($1) ~ /^testaccount2/ && /[0-9][0-9][0-9][0-9]/ {print $(NF-1)}'
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5
The above worked because the ip addess/ip address reside in the next to last field but what do I do if the field is in the last field. Is there a way I can tell AWK once it finds the account name and the ip adress/ip adresses to print out "only the ip adress/ip adresses" no matter where they are in the sting?

Last edited by metallica1973; 07-06-2012 at 02:28 PM.
 
Old 07-06-2012, 03:00 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You've set the delimiter to : the IP field seems to be 6 in your example. If that is always the case:
Code:
awk -F ':' 'tolower($1) ~ /^testaccount2/ && /[0-9][0-9][0-9][0-9]/ { print $6 }' wspasswd
$1 is first field, $2 second etc ($0 is the complete line).
 
Old 07-06-2012, 03:18 PM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
Many thanks,

That would apply if ip address/ip addresses field was always the 6th field, but will vary from the last field to next to the last field.

Code:
testaccount:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5

testaccount2:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:reminder2012 $56.00

testaccount3:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:$56.00
is there a way to have AWK look for the account name "search for ip address/ip addresses then search for the ip address/ip adddresses and print what is returned

Code:
cat wspasswd| awk -F ':' 'tolower($1) ~ /^testaccount2/ && /[0-9][0-9][0-9][0-9]/ /[0-9][0-9][0-9][0-9/ {print $(NF)}'

Last edited by metallica1973; 07-06-2012 at 03:20 PM.
 
Old 07-06-2012, 03:25 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
If the examples you give are correct, the IP field is always the sixth field. Sometimes the sixth field is the last field and sometimes there are more fields, but that doesn't matter.

Or provide example data that reflects the real data.

Last edited by druuna; 07-06-2012 at 03:29 PM. Reason: spelling
 
Old 07-06-2012, 03:39 PM   #5
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
Another forum has suggested this but it doest appear to work. It prints nothing.

Code:
cat wspasswd | awk -F: 'tolower($1) ~ /^testaccount2/{for(i=1;i<=NF;i++){if($i ~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/) print $i}}'
?
 
Old 07-06-2012, 03:40 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Try this:
Code:
awk -F ':' 'tolower($1) ~ /^testaccount2/ { for ( i = 1; i <= NF; i++ ) { if ( $i ~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ ) print $i } }' wspasswd
Little less accurate, but should work.

Last edited by druuna; 07-06-2012 at 03:41 PM. Reason: first search is unneeded.
 
Old 07-06-2012, 04:00 PM   #7
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
Code:
testaccount:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.21,192.168.14.26,192.168.21.93,192.168.41.44,192.168.41.95

testaccount2:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:reminder2012 $12.00

testaccount3:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.154.12.11,192.154.13.26,192.154.12.13,192.154.17.49,192.154.17.53:$7500.00

testaccount4:S#$G%^UJHG:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.51,192.168.1.52,192.168.1.53,192.168.1.54,192.168.51.50:$5600.00

testaccount5:11/242012:192.168.12.51,192.168.12.52,192.168.12.53,192.168.12.54,192.168.12.50
Sorry, as you can see from above the ip address it can be in various different fields {$3,$6}. Is there a way I can tell AWK once it finds the account name and the ip adress/ip adresses to print out "only the ip adress/ip adresses" no matter where they are in the string

Last edited by metallica1973; 07-06-2012 at 04:02 PM.
 
Old 07-06-2012, 04:08 PM   #8
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
thanks for all your help/

After I took a closer look at the data file, the 6th data field always reflects the ip address/ip addresses so your example will work, also your adjustment works for my original questions.

Code:
awk -F ':' 'tolower($1) ~ /^testaccount2/ { for ( i = 1; i <= NF; i++ ) { if ( $i ~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ ) print $i } }' wspasswd
Your awesome!!!!!

Last edited by metallica1973; 07-06-2012 at 04:13 PM.
 
Old 07-07-2012, 12:04 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,413

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
I would just add that the reason the earlier example did not work is due to the intervals being used. Anything prior to version 4 of gawk will require the additional switch of --re-interval
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk search on a variable dazdaz Programming 4 04-15-2012 06:21 AM
Help with awk or sed search. csbushy Linux - Newbie 5 12-14-2011 03:46 AM
How to search a pattern only first line using awk debianD Programming 8 08-02-2011 08:19 PM
awk search and insert? dyq Linux - Newbie 4 02-17-2010 12:04 AM
Need a less restrictive SMTP account (SBC just became too restrictive) jgombos Linux - Networking 2 04-26-2008 09:30 PM


All times are GMT -5. The time now is 07:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration