AWK Restrictive Search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Restrictive Search
# 1  
Old 07-06-2012
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
# 2  
Old 07-06-2012
Code:
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}}' inputfile

These 2 Users Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-06-2012
Thank you for you reply but it did not work.

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}}'

it prints nothing.

---------- Post updated at 05:16 PM ---------- Previous update was at 04:35 PM ----------

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

This did many many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Restriction of adding links is too restrictive

I am trying to write a shell script that generates links to a website. Not to spam it. The code necessarily adds things that are links, and so get rejected. In the end I have up, and have not written the post. Whilst I apprecaite you want to avoid spam, could you not implement something so... (1 Reply)
Discussion started by: drkirkby
1 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

4. Shell Programming and Scripting

Search and replace with awk

Hi Shell Tigers, I am trying to acheive search and replace strings within a setup file. Help much appreciated. test.setup ORACLE_HOME=/oracle/product/11.0.0/home01 PATH1=/perm_loc/3222/FTP/cfg1 PATH2=/perm_loc/3222/FTP/cfg2/bin PATH3=/perm_loc/3222/FTP/cfg3/bin So... (3 Replies)
Discussion started by: jville
3 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. UNIX for Dummies Questions & Answers

Awk search help

Hi, Want to know what does it returns. $ awk '$1 == /Jan/' inv $ awk '$1 = /Jan/' inv --- What does this one signify. 1 13 25 15 115 1 21 36 64 620 $ awk '$1 ~ /Jan/' inv Jan 13 25 15 115 Jan 21 36 64 620 Thanks in advance. (3 Replies)
Discussion started by: vanand420
3 Replies

7. Shell Programming and Scripting

awk search if else

Hi, I want to search for a particular string using awk and print "Found" or "Not Found" depending on the search. On searching this forum i got this code but it is not working: (6 Replies)
Discussion started by: rishav
6 Replies

8. Shell Programming and Scripting

awk search

i guys, i have a bash script , and it works, but i need an awk file, and i can't convert this code like: #!/bin/awk -f ..... my script #!/bin/bash awk '/\<FIRST\>|\<SECOND\>|\<THIRD\>|\<ZERO\>/' DOC.txt thanks :) (4 Replies)
Discussion started by: felito
4 Replies

9. UNIX for Advanced & Expert Users

Restrictive mail implementation problem

Any tips on this problem will be greatly appreciated. I need to build a Linux mailserver, that needs to meet the following requirements: 3 usergroups: endusers, supervisors, and management Endusers will be on a local Linux mailserver Supervisors and management will be on Google Apps ... (0 Replies)
Discussion started by: zefflyn
0 Replies
Login or Register to Ask a Question