Reputation: 45
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
Upvotes: 1
Views: 332
Reputation: 16016
You can use the awk gsub()
(global substitute) function to:
.
with space:
and /
cut -f4 test170201.rawtxt | awk '/stream_0/ {
gsub(/\./," ",$1)
gsub(/[:\/]/, "", $1)
print $1, $5
}' > testLogFile.txt
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 > testLogFile.txt
Upvotes: 3
Reputation: 1642
No problem, this sed
command in your pipe should do the trick:
awk '/stream_0/ { print $1, $5 }' test170201.rawtxt |
sed 's=/==g;s=:==g;s=\.= =g;s=^= =' > testLogFile.txt
Note that .
is a regular expression, so you must escape it to get sed
to recognize it as a period character. The g
modifier on the substitute commands tells sed
to remove all occurrences. Finally, the '^' regular expression matches the start of the line; that last clause puts the leading space into the output. The cut
command appears to be redundant.
See man sed
for more.
Upvotes: 2
Reputation: 785781
You can use gsub
in awk command before printing your columns:
awk '/stream_0/ { gsub(/[\/:]/, "", $1); gsub(/[.]/, " ", $1); print $1, $5 }' file
170201 105825 212577 0
170201 105825 213401 0
170201 105825 215560 0
170201 105825 216645 0
Upvotes: 2