Omri Omri - 1 year ago 47
Linux Question

Regex: Get a month of each line

I have a comma delimited log file. I need to get the month of each line. The date is in the third column of the file in the following format: 2016-07-11 02:11:43.

The following command will extract the whole date of the third column.

cut -d', ' -f3 logFile > newlogFile

How can i add a regex to this command to get just the mouth, instead of the whole date? (2016-07-11 02:11:43 --> 07).

Answer Source

You can use awk instead of cut and use split to parse month:

awk -F ', ' '{split($3, a, /[- ]/); print a[2]}' logFile > newlogFile