0

Suppose I have a file which is getting appended in every seconds/minutes. At first time I have read whole file and stored the last line number(eg: last_line_numer was : 2000) later I want to read file using 'grep' command to look for a pattern from line number 2001 to end of file. How we can achieve this?

First time I ran below command:

grep 'tracker.php' apache.log | cut -d' ' -f1-8

Now I want to ran above command with specific line number in grep param.

0

You can use tail to output the file from line number 2001 and pipe the result to your command:

<apache.log tail +2001 | grep 'tracker.php' | cut -d' ' -f1-8
  • When I use command like: grep -n 'tracker.php' apache.log | cut -d' ' -f1-8 then it gives line number from apache.log file like: 141007:50.57.61.8 - - [04/Apr/2019:09:52:01 -0400] "GET /tracker.php?q=1&p=2 HTTP/1.1" 141008:50.57.61.9 - - [04/Apr/2019:09:52:01 -0400] "GET /tracker.php?q=1&p=2 HTTP/1.1" but when I use: <apache.log tail +141009 | grep -n 'tracker.php' | cut -d' ' -f1-8 then it give line number 1,2,3 but not 141009, 141010, 141011. How can I get this? – groy May 4 at 20:42
  • cat -n apache.log | tail +2001 | grep -o '.*tracker.php[^"]*"'if we take the logfile from your other question – Freddy May 4 at 21:56
  • I am not able to use this command by storing value in variable , here is ref: stackoverflow.com/questions/56066661/… What can be issue here? – groy May 10 at 1:51
0

You can use

awk 'NR<2001 {next} /tracker.php/ {print NR, $1,$2,$3,$4,$5,$6,$7,$8}' apache.log

or

awk 'NR<2001 {next} /tracker.php/ {print NR, $0}' apache.log | cut -d' ' -f1-9

which will also display the original line numbers of the matching lines in the file.

  • 1
    Or awk 'NR>2000&&/tracker.php/ {NF=8; print NR,$0}' – dave_thompson_085 May 5 at 4:59
0

Using sed:

sed '1,2000d; /tracker\.php/!d' | cut -d ' ' -f-8

That is, discard lines 1 through to 2000 (inclusively), and then also discard any line not containing the string tracker.php. Send the rest of the lines through cut.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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