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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk to search adn delete char

Status
Not open for further replies.

simpson

MIS
Oct 23, 2001
58
CA
I would like to use an awk statement to do the follow. Search for a specific line in a file that contains a word and print out the 5th item.

Easy enough. awk '/foo/ {print $5}' /tmp/log

what I would like to do is take it one step further and truncate the result down because the data is wrapped in brackets. example.
# awk '/foo/ {print $5}' /tmp/log
returns: [255.255.255.0]

I would i like to strip off the leading and trailing bracket.

Any assistance woudld be appreciated.
thanks


 
This should do the trick:

Code:
awk '/foo/ {gsub("[][]","",$5);print $5}' /tmp/log

You may need to use nawk depending on your OS. nawk on Solaris doesn't seem to like the above character class syntax, in which case try:

Code:
nawk '/foo/ {gsub("[[]","",$5);gsub("[]]","",$5);print $5}' /tmp/log

Annihilannic.
 
awk '/foo/ {gsub("[][]","",$5);print $5}' /tmp/log

FANTASTIC.
Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top