Read the specified line number from file

Hi Guys,
I am new to unix.
Actually i want help in writing an single command where
i can actually read specific line number in file where the line number will be passed to command as parameter.

ex.
1 a
2 b
3 c
4 d

And to my command i pass as 2.
so i should get output as 2 b

---------- Post updated at 05:28 PM ---------- Previous update was at 05:25 PM ----------

i implemented this by below code

head -n parameter | tail -n 1

but if file is big this will degrade the performance.

But i need to have this as in single line only.
And if possible without awk.

sed '2!d' yourfile

Number = your wanted line number.

Hi.

n=3
$ sed "${n}!d" file
or
$ sed -n "${n}p" file
or
awk -v n=$n 'NR == n' file

Hi zaxxon,

Thanks for reply.
Can you please explain me what this command does.
i tried reading man sed in shell but it didn't help

$ num=3
$ gawk -v n=$num 'NR==n{print;exit}' file

num=3
cat -n samplefile.txt | grep " $num" | cut -f2

You might want to check websites for sed tutorials etc. or get a good book about it like sed&awk from O'Reilly.

It means

  • 2 ____ when the address is line 2
  • !d ____ do not delete it - ie. it will be printed

The opposite would have been:

sed -n '2p' infile

Which have meant to not print out every line but only the address you want to process.

Or you can use Perl:

$
$ 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

It seems much wiser to use

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.

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.

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.

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. :wink:
Thanks for reply
Regards.

The statement I made initially was based on this:

$ 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

Hello Scottn,
Sure the o/p speaks the truth.
:slight_smile:

---------- Post updated at 03:22 AM ---------- Previous update was at 03:20 AM ----------

Hello
because deleting every line has some overhead.
No doubts about it. In both the check operation is present in every iteration but in "p" there is one-time printing while "d" there is always deleting causing extra overhead and time.
:slight_smile:

It would expect the two statements to be identical in efficiency, after all in this context deleting means not printing and printing means not deleting.