Reading a file after nth line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a file after nth line
# 1  
Old 07-29-2011
Reading a file after nth line

I know how to read a file line by line. But don't to how to skip to a line matching a criteria and then continue reading it till the end.

This is a log file. The input is a timestamp.
1. Find the timestamp in the log file
2. Read the remaining lines one at a time till EOF.

How can I do this?
# 2  
Old 07-29-2011
Code:
sed -ne '/timestamp/,$p' yourfile

This User Gave Thanks to pludi For This Post:
# 3  
Old 07-29-2011
Quote:
Originally Posted by pludi
Code:
sed -ne '/timestamp/,$p' yourfile

you mean, p will have the rest of the file and i read it line by line?
# 4  
Old 07-29-2011
No. It's a sed script, telling it not to print the contents of the file, until the timestamp is found. From there on to the rest, print the file as it is.
# 5  
Old 07-29-2011
Ok here is my code
Code:
while read line
        do
                        processLine
        done < $logFile

Now where and how does your statement fit here?

---------- Post updated at 05:49 PM ---------- Previous update was at 05:34 PM ----------

I haven't worked much in unix, so finding it hard to perform simple operations

Last edited by radoulov; 08-01-2011 at 04:14 AM.. Reason: Code tags.
# 6  
Old 07-29-2011
If you want to do some "processing" on lines read after "timestamp" occured,

Code:
 
sed -ne '/'"${timestamp}"'/,$p' yourfile | while read line 
do
## do processing
done


Last edited by panyam; 07-29-2011 at 10:23 AM.. Reason: Changed as per requirement
This User Gave Thanks to panyam For This Post:
# 7  
Old 07-29-2011
Quote:
Originally Posted by panyam

Code:
 
sed -ne '/timestamp/,$p' yourfile | while read line 
do
## do processing
done

I am trying use a variable for timestamp like
Code:
sed -ne '/$timestamp/,$p' yourfile | while read line

And sed is not matching any records. I tried '$timestamp', "$timestamp"....nothing seems to be working.

---------- Post updated at 06:53 PM ---------- Previous update was at 06:48 PM ----------

I tried
Code:
sed -ne '/2011\/06\/28 11\:16\:14/,$p' test.log

on command prompt and it works fine.

Last edited by radoulov; 08-01-2011 at 04:15 AM.. Reason: Code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

Print nth line in a file

Bash/Oracle Linux 6.4 A basic requirement. How can I get nth line of a file printed ? Can I use grep in this case ? Example: In the below file, 12th line is "Kernel parameter check passed for rmem_max" . I just want the 12 line to be printed. # cat sometext.txt Kernel version check... (2 Replies)
Discussion started by: John K
2 Replies

4. Shell Programming and Scripting

Read a file from the nth line on

I have a script which reads from a job file and executed the scripts in the job file in sequence. #! /bin/ksh set -x while read line do $line.ksh if # mail the team fi done <"$file" The job file will be like abcd efgh ijkl mnop qrst This is working fine. I need to add... (2 Replies)
Discussion started by: nw2unx123
2 Replies

5. Shell Programming and Scripting

Get the nth word of mth line in a file

Hi.. May be a simple question but I just began to write unix scripts a week ago, for sorting some huge amount of experiment data, so I got no common sense about unix scripting and really need your helps... The situation is, I want to read the nth word of mth line in a file, and then store it... (3 Replies)
Discussion started by: freezelty
3 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. Shell Programming and Scripting

how to find third(nth) word in all line from a file

For example i'm having the below contents in a file: expr is great when you want to split a string into just two parts. The .* also makes expr good for skipping a variable number of words when you don't know how many words a string will have. But expr is lousy for getting, say, the fourth word... (2 Replies)
Discussion started by: bangarukannan
2 Replies

9. Shell Programming and Scripting

displaying nth line of a file dynamically

Hi, I have a file split.txt with the following contents one two three four five Suppose if i want to display contents of line 3, I know this could be achieved using the command sed -n '3p' split.txt But I need the line number to be decided dynamically like a=3 sed -n '$ap'... (2 Replies)
Discussion started by: deanamrita
2 Replies

10. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies
Login or Register to Ask a Question