0

I have a large text file with logs. After I execute the below awk command, I'm getting below output

grep TEST-FIELD test.log | awk '{print $23,$24,$4,$6,$7,$11,$12,$13}'


2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx

I need to change the output of the date column ($23) to like this

2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx

I tried to below command but it not printing proper values at all

grep TEST-FIELD test.log | awk  -F"/" '{OFS="/"; $23=strftime("%Y-%m-%d", $23); print $24,$4,$6,$7,$11,$12,$13}'

How can I get the correct output only using AWK

0

2 Answers 2

4

Change your original awk to:

awk '{gsub ("/", "-", $23); print $23,$24,$4,$6,$7,$11,$12,$13}'
0

command

awk '{x=substr($1,1,4)"-"substr($1,6,2)"-"substr($1,9,2);$1="";print x$0}'  filename

output

2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .