date - print string between two patterns but the first pattern is a range -
date - print string between two patterns but the first pattern is a range -
i have file containing dates in format mmddyyyy
.
the file cotents below:
05192014 10212014 10222014 11232014 12242014
now wanted print dates have month 10 or 11 , year 2014, or can dates between months ranging 01 11.
for single month , year command working fine:
cat filename | sed -n '/10/,/2014/p'
the above command prints:
10212014 10222014
all good!
but if wanted dates having month either 10 or 11 did below:
cat filename | sed -n '/1[0-1]/,/2014/p'
but above command prints dates. there way out. can set range in first pattern.
also, should able dates when specify months in range example:
print dates month greater equal 01 , less equal 11 , year 2014.
then output should be:
05192014 10212014 10222014 11232014
i guess want this:
$ sed -n '/^1[01].*2014$/p' file 10212014 10222014 11232014
^1[01].*2014$
means: strings starting 1
followed either 0
or 1
, bunch of strings end 2014
.
if want check dates on format mmddyyyy
, instead of ^1[01].*2014$/
can utilize ^1[01]..2014$
. using ^
, $
avoid matching lines hello11112014bye
, on.
also, note there no need cat ... | sed
. sed ... file
enough.
note current command not doing expect: sed -n '/10/,/2014/p' file
prints files 1 containing 10
1 containing 2014
, in between appear. see:
$ cat 05192014 10212014 asdfad 10222014 11232014 12242014 $ sed -n '/10/,/2014/p' 10212014 asdfad 10222014
also, should able dates when specify months in range example: print dates month greater equal 01 , less equal 11 , year 2014.
$ sed -rn '/^(0[1-9]|1[01]).*2014$/p' file 05192014 10212014 10222014 11232014
date sed
Comments
Post a Comment