I am trying to parse a log file, where I would like to get the first column and the text which follows a pattern.

The log file would look like this,

2018-04-25 00:00:10,139 INFO  [HiveServer2-HttpHandler-Pool: Thread-2678420]: parse.ParseDriver (ParseDriver.java:parse(185)) - Parsing command: show databases

I would like the final output which contains timestamp ( first and second column ) and all commands which is followed by keyword command:.

Is there a clean way of getting this? Currently, my plan is to use awk to fetch columns 1 and 2. Use sed to get the commands and then to merge the two outputs to a new file.

  • This seems a log from tomcat/wildfly you can alternativelly export it with with open office calc and the colums will be sorted for you. Else show me some code so I can help. – vfbsilva Apr 30 '18 at 4:05

This should do

awk -F'(,|command:)' '{print $1, $NF}' infile
2018-04-25 00:00:10  show databases

Here we defined field separators either a comma , or string command:, then print the first and last fields.


You can also do it with cut:

cut -d' ' -f1,2,12- ${YOUR_LOG_FILE}

Or (safer):

cat ${YOUR_LOG_FILE} | tr -s " " | cut -d' ' -f1,2,11-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.