Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

emphasized textI have a file containing 14000 dates . I wrote a script to find the last 5 days ,


suppose this is the file , now I have date 02/04/2002:11:30:41 as an out put . I want to put the dates from 02/04/2002 till the end of the file in another file .

start-date = 02/04/2002 (this is my start date) 
while [start-date lt end-date] do (while start date is less than end date )
start-date++ ( add one day to start day so if its 2/4/2002 it will become 3/4/2002)
echo $start-date|tee -a file1  (put it in a file)
share|improve this question
Stackoverflow would probably be the more appropriate forum for this, but I could be wrong. – Rilindo Sep 23 '11 at 14:13
Is it sorted in ascending order? I see the 26/03/2002:11:30:41 at the middle. – quanta Sep 23 '11 at 14:17
yes the file is sorted ascending – matarsak Sep 23 '11 at 14:24
up vote 1 down vote accepted

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


  • $ 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, ...

share|improve this answer
I did not get what you wrote , my matching pattern is a date which is in $SDATE and my input file which should be checked is a logfile; I check this code but it was not right, it suppose to print the last 5 days and the start date is located in $SDATE – matarsak Sep 23 '11 at 17:29
i check it , it was wrong ! :( grep -n $SDATE aa.log | cut -d: -f1 sed '/$SDATE/=' aa.log awk '/$SDATE/{ print NR }' aa.log $ sed -n "$(awk '/02\/04\/2002:11:30:41/ { print NR }' aa.log),$ p" aa.log $ awk 'NR >= line_number' line_number=$(grep -n 02/04/2002:11:30:41 -m 1 aa.log | cut -d: -f1) aa.log – matarsak Sep 23 '11 at 17:36
You don't understand what you're doing. Use one of the aboves, not all. – quanta Sep 24 '11 at 0:38
I wrote sed -n "$(awk '/01\/04\/2002:11:30:41/ { print NR }' thttpd.log),$ p" thttpd.log as you said but again it has problem ! sed: -e expression #1, char 1: unknown command: `,' – matarsak Sep 24 '11 at 15:55
The reason is you have multiples line including 01/04/2002:11:30:41, so the best way is use grep -n 02/04/2002:11:30:41 -m 1 to print only the first maching. – quanta Sep 24 '11 at 16:16

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.