0

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)
matarsak
  • 37
  • 5

2 Answers2

1

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

quanta
  • 51,413
  • 19
  • 159
  • 217
  • 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 00: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
  • I could not run this command ! while i run it i face with error command not found ! awk 'NR >= line_number' line_number=$(grep -n 30/Dec/2002:22:01:25 -m 1 thttpd.log | cut -d: -f1) thttpd.log – matarsak Sep 26 '11 at 18:30
1

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
hroptatyr
  • 191
  • 1
  • 4