Get only part of a line using awk

I have a log file and I am using awk to fetch the columns I need. My team

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

      

The $ 5 column contains the server name, and the format can be @: server1 @: @: @: server2 @: @: @: @ :, @: @: Server3 without a fixed number of @: characters.

How do I change my operator to remove all "@:" from column 5 to get only the server name?

+2


source to share


2 answers


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.

+3


source


I would suggest something like this:

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

      



Note that you can also write print $2,$5,$6

- ',' is replaced by the output field separator, which is a space by default.

(edited to remove "@:" from end of line)

0


source







All Articles