tags:

views:

74

answers:

2

I have a log file and i use awk to extract the required columns. My command is

awk '{ print $2" "$5" " $6}' log.txt

The $5 column contains the servername and the format can be like @:server1@:@:@:, server2@:@:@:@:@:, @:@:Server3 with no fixed amount of @: symbols.

How do I modify my statement so that I remove all the '@:' from the column 5 to get only the server name?

+1  A: 

You can use the gensub function.

awk '{ print $2, gensub(/@:/, "", "g", $5), $6}' log.txt

EDIT: See glenn jackman's comment, below, for possible portability implications.

Jonathan Feinberg
Is `gensub()` only in gawk? `gsub(/@:/, "", $5)` is "portable"
glenn jackman
Ouch, you're probably right.
Jonathan Feinberg
A: 

I'd suggest something like this:

awk '{ sub("^(@:)*","",$5); sub("(@:)*$","",$5); print $2" "$5" "$6 }'

Note that you can also write print $2,$5,$6 - the ',' is replaced by the output field separator, which is by default a space.

(edited to remove "@:" from the end of the string as well)

Jefromi