Select matches between line number and end of file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select matches between line number and end of file?
# 1  
Old 05-23-2006
Select matches between line number and end of file?

Hi Guys/Gals,

I have a log file that is updated once every few seconds and I am looking for a way to speed up one of my scripts.

Basically what I am trying to do is grep through a text file from start to finish once. Then each subsequent grep starts at the last line of the previous grep to the end of the file.

The log file can get as large as 200MB and rather then going through the file start to finish every time, I want to be able to have the second grep start off where the last grep finished, cutting down on processing time.

I tried to use tail to grab the last x amount of lines of the file which is much faster, but the issue is the file keeps being updated constantly so the last 100 lines is only valid for about 3 seconds, until a new line gets appended to the log file.

Is there a command to tail line 100101 to end_of_file?

I figured I could determine the last line number by using something like this
cat -n logfile.txt | tail -n1 | gawk "{print $1}" > lastline.txt

If anyone has a suggestion on how to accomplish this, please share your thoughts.

Thanks

Jerrad
# 2  
Old 05-23-2006
Wow. 200MB?? That's a huge text file. Personally, I would first rotate that log to a more manageable size first.

... you could use the tail command with the + operand..
...so you'd do something like this ( not complete code, but you'll get the idea):

Code:
 # cat logfile | wc -l  >> marker.txt  ## where marker has the number of lines in
                                     ## your logilfe at the end of  your script

... then. next time you run your script:
Code:
# COUNT=$(cat marker)
let MARKER=${COUNT}+1
#  tail +$(MARKER} logfile

...and you'll only get the lines in the logfile that were appended after the last time you checked.

I'd still strongly suggest you rotate that log file more often first. It's easier and faster to work with closed, smaller sized files.

... quick example of the difference between tail -n and tail +n :
..say you have a file:
Code:
# cat file
111111111111
2222222222222
33333333333333
44444444444444
555555555555555
666666666666666
777777777777777
88888888888888
999999999999999
aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc

#tail -5 file 
88888888888888
999999999999999
aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc

# tail +5 file
555555555555555
666666666666666
777777777777777
88888888888888
999999999999999
aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc


Last edited by System Shock; 05-23-2006 at 08:25 PM..
# 3  
Old 05-23-2006
Code:
sed -n '100101,$p'

# 4  
Old 05-24-2006
what if

i want to print from xth line to second last line?
that is $p in sed will be short by 1.
so how is the syntax?
# 5  
Old 05-24-2006
why cannot you use some thing like this.

tail -f name_of_your_file | grep 'text you want to search'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

4. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Shell Programming and Scripting

Get line number when matches a string

If I have a file something like as shown below, ARM*187878*hjhj BAG*88778*jjjj COD*7777*kkkk BAG*87878*kjjhjk DEF*65656*89989*khjkk I need the line numbers to be added with a colon when it matches the string "BAG". Here in my case, I need something like ARM*187878*hjhj... (4 Replies)
Discussion started by: Muthuraj K
4 Replies

6. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

7. Shell Programming and Scripting

Capturing a number at the end of line and store it as variable

Hello, Would someone guide me on how to write a shell script the would search for a phone no using at the end text file using sed or awk and store it in a varaible or print it. The text file is in this form text or numbers in first line text or numbers in second line . . . Firsname... (6 Replies)
Discussion started by: amuthiga
6 Replies

8. Shell Programming and Scripting

How to Select or cut from the certain filed to the end of the line

Hi: I have few rows in file..Like suppose... 9063C0 44 00051363603253033253347 3333 070248 06 9063C0 5G PAN00013 9063C0 44 00061030305040404250724 0506 100248 08 9063C0 43 01 00000089 I need to cut the row starting after... (5 Replies)
Discussion started by: grajesh_955
5 Replies

9. Shell Programming and Scripting

Number a list at end of line using 'sed'

Hi All I have a script which has produced a list, I have used 'sed' to number my list, but i want to list at end of line with the first line starting at zero (0) and brackets round it ie My List i want Hello (0) this (1) day (2) can (3) be (4) sed '/./=' filename | sed '/./N; s/\n/) /'... (5 Replies)
Discussion started by: chassis
5 Replies

10. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies
Login or Register to Ask a Question