how to manage dates in shell

There are some ways to do this: grep, sed, awk, ...

You can get the line number of matching pattern by one of the following:

  • grep -n pattern -m 1 input | cut -d: -f1
  • awk '/pattern/{ print NR }' input | head -1

and print from that line to the end of the file:

  • $ sed -n "$(awk '/02\/04\/2002:11:30:41/ { print NR }' input | head -1),$ p" input

or:

  • $ awk 'NR >= line_number' line_number=$(grep -n 02/04/2002:11:30:41 -m 1 input | cut -d: -f1) input

You also can use grep -A (--after-context) or tail, ...


If you can do with an external helper tool, try dateutils. It comes with a dgrep command which does what you want:

dgrep '>=2002-04-02' -i '%d/%m/%Y' < yourfile
=>
  02/04/2002:11:30:41
  03/04/2002:11:30:41
  04/04/2002:11:30:41
  05/04/2002:11:52:25
  06/04/2002:11:52:25