user3325598 user3325598 - 4 years ago 95
Linux Question

Removing dots and slashes when piping into new file in bash (awk)

I have a bash question (when using

awk
). I'm extracting every single instance of the first and fifth column in a textfile and piping it to a new file with the following code,

cut -f4 test170201.rawtxt | awk '/stream_0/ { print $1, $5 }' > testLogFile.txt


This is part of the file (test170201.rawtxt) I'm extracting the data from, columns
Timestamp
and
Loss
,

Timestamp Stream Status Seq Loss Bytes Delay
17/02/01.10:58:25.212577 stream_0 OK 80281 0 1000 38473
17/02/01.10:58:25.213401 stream_0 OK 80282 0 1000 38472
17/02/01.10:58:25.215560 stream_0 OK 80283 0 1000 38473
17/02/01.10:58:25.216645 stream_0 OK 80284 0 1000 38472


This is the result I'm getting in testLogFile.txt

17/02/01.10:58:25.212577 0
17/02/01.10:58:25.213401 0
17/02/01.10:58:25.215560 0
17/02/01.10:58:25.216645 0


I want to get rid of the
"/"
,
":"
and the
"."
. The result output I want would look like this:

170201 105825 212577 0
170201 105825 213401 0
170201 105825 215560 0
170201 105825 216645 0


My question is, how do I modify my code to get the above result? The platform I'm using is Debian GNU/Linux and the GNU awk version is 4.0.1

Answer Source

You can use the awk gsub() (global substitute) function to:

  • substitute . with space
  • remove : and /
cut -f4 test170201.rawtxt | awk '/stream_0/ { 
    gsub(/\./," ",$1)
    gsub(/[:\/]/, "", $1)
    print $1, $5
}'

In fact, the cut seems to be redundant - this will do what you require with the example data:

awk '/stream_0/ { 
    gsub(/\./," ",$1)
    gsub(/[:\/]/, "", $1)
    print $1 , $5
}' test170201.rawtxt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download