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

26/03/2002:11:52:25
27/03/2002:11:52:25
29/03/2002:11:30:41
30/03/2002:11:30:41
26/03/2002:11:30:41
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

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)
link|improve this question

67% accept rate
1  
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
feedback

1 Answer

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

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

link|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
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

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