How do i extract the text present in the 7th line of a file using shell script For eg., I have sumthing like,
abc
def
ghi
jkl
mno
pqr
stu
I need to print the text stu
.
Can sumbdy help on this. Pls...
How do i extract the text present in the 7th line of a file using shell script For eg., I have sumthing like,
abc
def
ghi
jkl
mno
pqr
stu
I need to print the text stu
.
Can sumbdy help on this. Pls...
You can make it with awk
:
awk 'NR==7' file
as NR
refers to number of line.
Also in sed
:
sed -n '7p' file
Update And even better (thanks pixelbeat)
sed -n '7{p;q}' file
Let's fill a file with 1,000,000 lines:
$ for i in {1..1000000}; do echo $i>>a; done
Now let's compare the time used by each sed
:
$ time sed -n '3p' a
3
real 0m0.098s
user 0m0.084s
sys 0m0.008s
$ time sed -n '3{p;q}' a
3
real 0m0.012s
user 0m0.000s
sys 0m0.008s
Which is 8 times faster!
$ echo "0.098 / 0.012" | bc
8
In addition to awk, there are many other utilities that can be composed to do this. Here are a few more:
head
and tail
head -n 7 file | tail -n 1
perl
perl -ne 'print if $.==7' file
ruby
is actually identical to the perl
ruby -ne 'print if $.==7' file
There is probably a shorter way, here it is in python:
python -c "import sys; x=[l for l in sys.stdin]; sys.stdout.write(x[6])" < x