Saran Brar
Saran Brar

Reputation: 147

Capture only hour and minute from logs. Want to exclude seconds in output

I am running below command to capture output. In time section seconds are also getting captured. I want to exclude seconds in output. Please help

cat /tmp/dnsmasq.log | grep query | egrep -iv 'AAA|PTR|SRV' | awk '{print $1" "$2" "$3","$8","$6}' | awk 'BEGIN{FS=OFS=","} {num=split($NF,arra y,".");$NF=array[num-1]"."array[num]} 1'

Sample input file is below:

May 31 17:58:57 dnsmasq[1695]: query[A] sites.google.com from 10.0.0.35

May 31 17:59:15 dnsmasq[1695]: query[A] presence.teams.microsoft.com from 10.0.0.35

I want output like this:

May 31 17:49,127.0.0.1,scotiabank.com

May 31 17:49,10.0.0.35,google.com

Upvotes: 1

Views: 110

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133640

EDIT: Adding 1 more solution after seeing your Input_file but still not clear about conditions how to get it, try following once. Written and tested with shown samples only.

awk '
BEGIN{
  OFS=","
}
/query/ && match($0,/^[a-zA-Z]+ [0-9]{2} [0-9]{2}:[0-9]{2}/){
  print substr($0,RSTART,RLENGTH),$NF,$(NF-2)
}
'  Input_file


Since you have not shown your actual Input_file so following is written by seeing your shown command's output only.

your_command | awk 'BEGIN{FS=OFS=","} {sub(/:[0-9]{2}$/,"",$1)} 1'

IMHO you could do all these operations in a single awk itself if you show us the sample of Input_file, rather than using 3 to 4 pipe commands.

EDIT BY OP(Working command is): cat /tmp/dnsmasq.log| grep query | egrep -iv 'AAA|PTR|SRV' | awk '{print $1" "$2" "$3","$8","$6}' | awk 'BEGIN{FS=OFS=","} {sub(/:[0-9]{2}$/,"",$1)} 1' | awk 'BEGIN{FS=OFS=","} {num=split($NF,array,".");$NF=array[num-1]"."array[num]} 1' |sort | uniq

NOTE: This task could be done in a single awk but OP has added above commands into his/her existing code. For future users a single awk will be more efficient here.

Upvotes: 2

Related Questions