Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cut with a string field delimiter

Status
Not open for further replies.

jmiturbe

Technical User
Feb 4, 2003
99
ES
Hello,

How can I get some all the text of a line between two string field separators? For example, how do I get the IP number contained between the words "from" an "at" from the following log?

..........received from 192.168.1.10 at ....

I need to specify from and at as field separators because the log lines are irregular.

Thanks in advance,

jmiturbe
 
Try this
Code:
#!/bin/awk -f
match($0,"from.*at") > 0 {
  print substr($0,RSTART+4,RLENGTH-6)
}
 
To get everything between "from" and "at"...

awk '{ if (match($0,"from .* at"))
print substr($0,RSTART+5,RLENGTH-8) }' file1

To get all IP addresses in a file...

awk '{ if (match($0,"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"))
print substr($0,RSTART,RLENGTH) }' file1


 
Here's an example I tried at unix command line and it works:
echo ".....received from 192.168.1.10 at ...." | awk -F'(from|at)' '{print $2}'
 
And what about this ?
Code:
sed 's!^.*from !!;s! at.*!' /path/to/yourlogfile

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top