Skip to the content.

Home / cs-notes / Operating System / Linux / Commands / Problems / 输出文件指定行

一行

awk ‘NR == 10’ file.txt

sed -n ‘10p’ file.txt

tail -n+10 file.txt head -n1 不足 10 行时,不输出
head file.txt -n10 tail -n1 不足 10 行时,输出最后 1 行

范围行

awk ‘NR>=5 && NR <=8’ file.txt

sed -n ‘5,8p’ file.txt

sed -n ‘10,$p’ file.txt


head + tail 比 awk、sed 快很多


匹配行

sed ‘/pattern/p’ file.txt

sed ‘/pattern/!p’ file.txt