If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > extracting specific lines in a text file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-10-04, 13:14
hgwells3 hgwells3 is offline
Registered User
 
Join Date: Jul 2004
Posts: 6
extracting specific lines in a text file

Hi,

New to scripting. need to go through a text file and extract IP addresses for "Special Users" only as listed in example file below, and write only the IP Addresses to another file. Any help much appreciated.

File looks like this:

-- Primary users--------------------------------------------------
IPAddress | City | Country | Username | misc
-----------------------------------------------------------------
11.208.23.56 | NY | USA | user1 |
-----------------------------------------------------------------
-- Special users--------------------------------------------------
IPAddress | City | Country | Username | misc
-----------------------------------------------------------------
11.208.23.56 | NY | USA | user1 |
12.209.23.56 | LA | USA | user1 |
-----------------------------------------------------------------
Reply With Quote
  #2 (permalink)  
Old 07-10-04, 13:47
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
nawk -f hq.awk yourInput > yourSpecialOutput

here's hq.awk

Code:
BEGIN {
  FS="[|]"
  TAGusers="users"
  TAGspecialusers="Special users"
  PAT_IPaddress="[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"
}

$0 ~ TAGusers { block=0 }
$0 ~ TAGspecialusers { block=1 }

block && match($1, PAT_IPaddress) { print $1 }
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 07-10-04, 16:39
hgwells3 hgwells3 is offline
Registered User
 
Join Date: Jul 2004
Posts: 6
re:extracting specific lines in a text file

Wow, thanks! That works great. Is it possible to run that nawk body within a unix bash script, or must it be in separate file?

hg
Reply With Quote
  #4 (permalink)  
Old 07-10-04, 17:12
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
nawk '
BEGIN {
  FS="[|]"
  TAGusers="users"
  TAGspecialusers="Special users"
  PAT_IPaddress="[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"
}

$0 ~ TAGusers { block=0 }
$0 ~ TAGspecialusers { block=1 }

block && match($1, PAT_IPaddress) { print $1 }
' yourInput > yourSpecialOutput
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On