Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output lines 800-900 of a file with a unix command?

I want to output all lines between a and b in a file.

This works but seems like overkill:

head -n 900 file.txt | tail -n 100

My lack of unix knowledge seems to be the limit here. Any suggestions?

like image 867
Jesper Rønn-Jensen Avatar asked Oct 21 '09 09:10

Jesper Rønn-Jensen


People also ask

How do I print the number of lines in a file Unix?

The tool wc is the "word counter" in UNIX and UNIX-like operating systems, but you can also use it to count lines in a file by adding the -l option. wc -l foo will count the number of lines in foo .

How do I get the last 50 lines of a file in Unix?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.

How do I get the last 10 lines of a file in Unix?

Linux Tail Command Syntax Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits. as you can see, this prints the last 10 lines of /var/log/messages.


1 Answers

sed -n '800,900p' file.txt

This will print (p) lines 800 through 900, including both line 800 and 900 (i.e. 101 lines in total). It will not print any other lines (-n).

Adjust from 800 to 801 and/or 900 to 899 to make it do exactly what you think "between 800 and 900" should mean in your case.

like image 76
ndim Avatar answered Oct 14 '22 22:10

ndim