1. Get the K line (take k=10 as an example)
Note that if the file contains less than 10 rows, it should not be output.
# Read from thefile file. txt and output the tenth line to stdout.# solution one, using awkawk 'NR = = Ten' file. txt# solution Two, using sed (personal test Result: Sed method faster than awk)sed-N'10p' filetxt# Solution III, combined use of tail and headTail-n+Ten file. txt |Head-n1# In addition, the following methods do not meet the requirementsHead file. txt-n10 |Tail-N1 # Because if the file contains less than 10 rows, the last line is output
Also, output line 5th to line 8th:
awk ' nr>=5 && NR <=8 ' file. txt
The topic comes from the 195 of Leetcode. Tenth line
Solution Reference: http://bookshadow.com/weblog/2015/03/28/leetcode-tenth-line/
2. Get some rows
sed ' /pattern/!p ' file. txt //sed'5,9p'file. txt // output V and nineth sed'ten, $p'file. TXT // output line tenth to the last line
The specified line of the [Linux] output file