ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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"
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.
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
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
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.