help with sed

Hello, I have the following file:

ABCDE_CMS_10.SUNDSVALLALT.I_20120918_120601_P_0296_00.MBF
ABCDE_CMS.1.NOKPART.I_20120918_234811_P_44766_00.MBF
ABCDE_CMS.10.STOF.I_20120919_073742_P_6506.ict.zip
ABCDE_CMSAA.20120918_234810_P_44713_00.MBF
ABCDE_CMS.10.STOF.I_20120919_073742_P_6506.ict.zip
ABCDE_CMS_10.STOF.I.20120919_235242_P_6475_00.MBF
ABCDE_CMS_10.SUNDSVALLALT.I_20120920_120601_P_0296_00.MBF
ABCDE_CM10.STOF.I.20120920_233742_P_6474_00.MBF
ABCDE_CMS.10.STOF.I_20120920_235340_P_6682_00.MBF
ABCDE_CMS10.STOF.I.20120921_233840_P_6681_00.MBF
ABCDE_CMS.1.NOKPART.I_20120920_234811_P_44766_00.MBF
ABCDE_CMS.10.STOF.I_20120921_073742_P_6506.ict.zip
ABCDE_CMSAA.20120921_234810_P_44713_00.MBF

I am using the following command to get all lines that contain dates between 20120919 and 20120921:

sed -n "/20120919/,/20120921/p" input_file.txt

The problem is that it gets all lines from the first 20120919 to the first 20120921 and when I have 20120918 after 20120919 it also will be included /marked in blue/ and when I have 20120921 before other lines with 20120920 they will be skipped /marked in red/.

Any ideas how can I handle that?

Thanks in advance!

Hi apenkov,

Not an elaborated solution, but could work for this simple case. Try:

$ sed -n "/201209\(19\|20\|21\)/ p" infile
ABCDE_CMS.10.STOF.I_20120919_073742_P_6506.ict.zip
ABCDE_CMS.10.STOF.I_20120919_073742_P_6506.ict.zip
ABCDE_CMS_10.STOF.I.20120919_235242_P_6475_00.MBF
ABCDE_CMS_10.SUNDSVALLALT.I_20120920_120601_P_0296_00.MBF
ABCDE_CM10.STOF.I.20120920_233742_P_6474_00.MBF
ABCDE_CMS.10.STOF.I_20120920_235340_P_6682_00.MBF
ABCDE_CMS10.STOF.I.20120921_233840_P_6681_00.MBF
ABCDE_CMS.1.NOKPART.I_20120920_234811_P_44766_00.MBF
ABCDE_CMS.10.STOF.I_20120921_073742_P_6506.ict.zip
ABCDE_CMSAA.20120921_234810_P_44713_00.MBF

And, of course, there's grep, which is well suited for this kind of work.

$ grep -E "20120919|2012092[01]" infile

with awk..

awk -F "2012" '{s=substr($2,1,4);if( s >= "0919" && s <= "0921" ){print $0}}' file
1 Like

Hi, the problem is that there can be a lot of dates in the file, let's say for an year ago and start and end date are input from the user.

awk -v strt=20120919 -v end=20120921 'match($0,/_[0-9]{8}_/){
dt=substr($0,RSTART+1,8)
if(dt+0>=strt+0 && dt+0<=end+0) print}' file
1 Like

At least for this year it will work..:slight_smile:

awk -F "2012" -v FM="20120818" -v SM="20120921" '{s="2012"substr($2,1,4);if( s >= FM && s <= SM ){print $0}}' file
1 Like

Thanks pamu, it is working, I changed "2012","0919","0921" with the user input, so it works regardless the dates :slight_smile:

---------- Post updated at 02:38 PM ---------- Previous update was at 02:36 PM ----------

Thanks elixir_sinari, it is working and also easy to change strt and end with the user input