Read the specified line number from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read the specified line number from file
# 8  
Old 12-18-2009
Or you can use Perl:

Code:
$
$ cat f2
1   a
2   b
3   c
4   d
5   e
6   f
7   g
8   h
9   i
10   j
$
$ LNUM=7
$
$ perl -lne "$. == $LNUM && print" f2
7   g
$
$

tyler_durden
# 9  
Old 12-18-2009
Quote:
Originally Posted by zaxxon
Code:
sed '2!d' yourfile

Number = your wanted line number.
It seems much wiser to use
Code:
sed -n '2p' your_file

kinda printing that line instead of deleting the others.
Regards.
# 10  
Old 12-18-2009
Quote:
Originally Posted by gaurav1086
It seems much wiser to use
Code:
sed -n '2p' your_file

kinda printing that line instead of deleting the others.
Regards.
The two options (2p or 2!d) are identical. It's no more wise (or otherwise) to use one (or not) over the other. The only thing sed has to do is decide whether to print the line or not. It doesn't take sed longer to decide not to print it than to print it.
# 11  
Old 12-18-2009
Quote:
Originally Posted by scottn
The two options (2p or 2!d) are identical. It's no more wise (or otherwise) to use (or not) over the other. The only thing sed has to do is decide whether to print the line or not. It doesn't take sed longer to decide not to print it than to print it.
Hello Scottn,
when using "d" option the processing is sequential, i.e. that control iterates over and deletes over the lines except the ones specified not to. So the control has to iterate over the whole of the file and keep on deleting until the last record.
But when using "p" option as stated by me,
In the worst scenario (if you are not using hash) , if the control flows sequential
then the control iterates only until it reaches that specified line (in this case 2).
and then breaks off.
Even if the control flows to the last statement it doesnt have take any operation(unlike in the first case i.e. deleting).

Or otherwise I would take do it like this to optimize if over large files.
Code:
awk 'NR==2{print;exit}' your_file

PS. pick what you want, dont discard what you dislike.
# 12  
Old 12-18-2009
Quote:
Originally Posted by gaurav1086
Hello Scottn,
when using "d" option the processing is sequential, i.e. that control iterates over and deletes over the lines except the ones specified not to. So the control has to iterate over the whole of the file and keep on deleting until the last record.
But when using "p" option as stated by me,
In the worst scenario (if you are not using hash) , if the control flows sequential
then the control iterates only until it reaches that specified line (in this case 2).
and then breaks off.
Even if the control flows to the last statement it doesnt have take any operation(unlike in the first case i.e. deleting).

Or otherwise I would take do it like this to optimize if over large files.
Code:
awk 'NR==2{print;exit}' your_file

PS. pick what you want, dont discard what you dislike.
Hi gaurav1086.

That's all good and fine, but my statement is based on this post.

And in this post the two are identical.
# 13  
Old 12-18-2009
Computer

Quote:
Originally Posted by scottn
Hi gaurav1086.

That's all good and fine, but my statement is based on this post.

And in this post the two are identical.
Hello scottn,
yeah maybe for smaller files but take a file with some 100000 records and time each of them and I bet there would be atleast slight difference in the efficiency of the two. ;-) Image
Thanks for reply
Regards. Image
# 14  
Old 12-18-2009
Quote:
Originally Posted by gaurav1086
Hello scottn,
yeah maybe for smaller files but take a file with some 100000 records and time each of them and I bet there would be atleast slight difference in the efficiency of the two. ;-) Image
Thanks for reply
Regards. Image
The statement I made initially was based on this:

Code:
$ wc out.txt                
 1759303 4838085 23639491 out.txt
$ time sed -n 10000p out.txt
2501 Fri Dec 18 22:11:55 CET 2009 T

real	0m0.20s
user	0m0.18s
sys	0m0.01s
$ time sed 10000!d out.txt  
2501 Fri Dec 18 22:11:55 CET 2009 T

real	0m0.21s
user	0m0.18s
sys	0m0.02s
$ time sed 10000!d out.txt
2501 Fri Dec 18 22:11:55 CET 2009 T

real	0m0.20s
user	0m0.18s
sys	0m0.02s
$ time sed -n 10000p out.txt
2501 Fri Dec 18 22:11:55 CET 2009 T

real	0m0.18s
user	0m0.16s
sys	0m0.01s

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

5. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

7. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

10. Shell Programming and Scripting

How to get the line number from while read

I have a file TXTPROCESS.TXT.20071129 in which line 1 contains the file name and rest of the records are data. The file data looks like this: TXTPROCESS.TXT.20071129 DIVD20071129 INTR20071129 BALN20071129 From line 2 onwards the first 4 characters defines individual process. What I... (2 Replies)
Discussion started by: skymirror
2 Replies
Login or Register to Ask a Question