문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

Found a prettier way: Using sed, to print out only lines between a and b:

sed -n -e 800,900p filename.txt

From the blog post: Using sed to extract lines in a text file

One way I am using it is to find (and diff) similar sections of files:

 sed -n -e 705,830p mnetframe.css > tmp1; \
 sed -n -e 830,955p mnetframe.css > tmp2; \
 diff --side-by-side tmp1 tmp2

Which will give me a nice side-by-side comparison of similar sections of a file :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top