Ubuntu – Use sed and grep to extract data for particular months in a file with timestamps

command linegrepsedtext processing

I have text file with six years (2011-2016) of data. I want to extract only April and May data of all six years.

@STATION_ID,LATITUDE,LONGITUDE,TIME(GMT),DATE(GMT),AIR_TEMP(‌​°C) 
IMDE1611_14164B(PITAMPURA),28.7,77.15,0,08/09/2011,33.5 
IMDE1611_14164B(PITAMPURA),28.7,77.15,1,08/09/2011,33.3 
IMDE1611_14164B(PITAMPURA),28.7,77.15,2,08/09/2011,33.8 
IMDE1611_14164B(PITAMPURA),28.7,77.15,3,08/09/2011,33.8 
IMDE1611_14164B(PITAMPURA),28.7,77.15,4,08/09/2011,34.5 
IMDE1611_14164B(PITAMPURA),28.7,77.15,5,08/09/2011,35.0 
IMDE1611_14164B(PITAMPURA),28.7,77.15,6,08/09/2011,34.9 
IMDE1611_14164B(PITAMPURA),28.7,77.15,7,08/09/2011,35.4 

I am using grep and sed commands to filter the data but it is not showing the result I want.
I am using these commands:

grep "??-0[4-5]-????" filename.txt > filename.csv
sed -n '/2016-04-01/,/2016-04-30/{/2016-04-30/d; p}' my_delhi.txt
sed -n '/2016-04-01/,/2016-04-30/p' my_delhi.txt

Best Answer

You can use something else instead of / as sed's delimiter.

sed -n '\:08/09/2011:p' file

or with regex and '#' as delimiter:

sed -nr "\#,[0-9]{2}/[0-9]{2}/[0-9]{4},#p" file

for April and May only (dd/mm/yyyy):

sed -nr "\#,[0-9]{2}/0[45]{1}/[0-9]{4},#p" file

or (mm/dd/yyy):

sed -nr "\#,0[45]{1}/[0-9]{2}/[0-9]{4},#p" file
Related Question