Operating System - HP-UX
1827816 Members
2363 Online
109969 Solutions
New Discussion

Re: 10th line in a text file?

 
Beginner_2
Occasional Advisor

10th line in a text file?

how can we take out only 10th line from a text file using shell script
6 REPLIES 6
Steven Schweda
Honored Contributor

Re: 10th line in a text file?

Do you wish to extract like 10 or delete line
10?

$ sed -n -e '5p' < lines.txt
5

$ sed -n -e '1,4p' -e '6,$p' < lines.txt
1
2
3
4
6
7
8
9

$ cat lines.txt
1
2
3
4
5
6
7
8
9
James R. Ferguson
Acclaimed Contributor

Re: 10th line in a text file?

HI:

Perhaps the easiest way is:

# sed -ne '10p' file

...which says (p)rint the 10th (one-relative) line in the file while suppressing ('-n') the printing of all other lines.

If on the other hand you want to delete the 10th line, leaving the rest:

# sed -e '10d' file > file.new

...or delete a range of lines"

# sed -e '10,20d' file > file.new

...or delete from line-10 until the end of the file:

# sed -e '10,$d' file > file.new

Regards!

...JRF...
Paul Ettema
Advisor

Re: 10th line in a text file?

Hoi,

What about:

cat | head -10 | tail -1

Paul.
Raj D.
Honored Contributor

Re: 10th line in a text file?

# awk 'NR==10' file #Enjoy! Have fun!.
" If u think u can , If u think u cannot , - You are always Right . "
rariasn
Honored Contributor

Re: 10th line in a text file?

Hi,

First 10 lines:

$ head -10 file

Last 10 lines:

$ tail -10 file

rgs,
Peter Nikitka
Honored Contributor

Re: 10th line in a text file?

Hi,

use just the negation of Raj's one liner:
awk 'NR != 10' filename

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"