此命令显示文件的第二行:
cat myfile | head -2 | tail -1
Run Code Online (Sandbox Code Playgroud)
我的文件包含以下数据:
hello
mark
this is the head line
this is the first line
this is the second line
this is the last line
Run Code Online (Sandbox Code Playgroud)
上面的命令将数据打印为: mark
但我无法理解这一点,因为,头-2用于打印前两行而尾-1打印最后一行但是如何打印第二行!! ???
pau*_*sm4 17
您还可以使用"sed"或"awk"打印特定行:
例:
sed -n '2p' myfile
Run Code Online (Sandbox Code Playgroud)
PS:关于"我的'head | tail'"命令有什么问题 - shelltel是正确的.
per*_*eal 14
tail显示头输出的最后一行,head输出的最后一行是文件的第二行.
头部输出(输入到尾部):
hello
mark
Run Code Online (Sandbox Code Playgroud)
尾部输出:
mark
Run Code Online (Sandbox Code Playgroud)
你可以使用sed命令。以下是打印行号的方法,您可以根据您的任务进行选择。
sed -n '2p' filename #get the 2nd line and prints the value (p stands for print)
sed -n '1,2p' filename #get the 1 to 2nd line and prints the values
sed -n '1p;2p;' filename #get the 1st and 2nd line values only
Run Code Online (Sandbox Code Playgroud)