Skip to content
Advertisement

Extracting month from day using linux terminal [closed]

I am having a text file containing a list of date and time just like the sample below –

posted_at”
2012-06-09 11:48:31″
2012-08-09 12:40:02″
2012-04-09 13:10:00″
2012-03-09 13:40:00″
2012-10-09 14:30:01″
2012-12-09 15:30:00″
2012-11-09 16:20:00″

I want to extract the month from each line.

P.S – grep should not be used at any point of the code Thanks in advance!

Advertisement

Answer

First select date pattern :

egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file

Second, extract the month :

awk -F '-' '{print $2}'

Third redirect to desired file :

>> desired_file

So mix of all this with | to final solution :

egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file| awk -F '-' '{print $2}'>> desired_file

VoilĂ 

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement