Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
unix-显示文件的第二行_Unix - Fatal编程技术网

unix-显示文件的第二行

unix-显示文件的第二行,unix,Unix,此命令显示文件的第二行: cat myfile | head -2 | tail -1 我的文件包含以下数据: hello mark this is the head line this is the first line this is the second line this is the last line 上面的命令将数据打印为:mark 但我无法理解这一点,因为头-2用于打印前两行,尾-1用于打印最后一行,但为什么要打印第二行呢 tail显示头输出的最后一行,头输出的最后一行是

此命令显示文件的第二行:

cat myfile | head -2 | tail -1
我的文件包含以下数据:

hello
mark
this is the head line
this is the first line 
this is the second line 
this is the last line
上面的命令将数据打印为:
mark


但我无法理解这一点,因为头-2用于打印前两行,尾-1用于打印最后一行,但为什么要打印第二行呢

tail显示头输出的最后一行,头输出的最后一行是文件的第二行

linux> tail -1 /tmp/xx
mark
头部输出(尾部输入):

尾部输出:

mark
您还可以使用“sed”或“awk”打印特定行:

例如:

sed -n '2p' myfile
附言:
至于“我的‘头’|尾’有什么问题”命令,shelltel是正确的。

如果你将操作分解为单独的命令,那么很明显它为什么会这样工作

head-2创建一个包含两行的文件

linux> head -2 /tmp/x > /tmp/xx
linux> cat /tmp/xx  
hello
mark
tail-1打印出文件中的最后一行

linux> tail -1 /tmp/xx
mark

tail仅对head的输出进行操作,
tail-1
表示打印接收到的最后一行输出。在此上下文中(在管道中),
tail
不知道myfile存在。您的管道正在按设计工作,您观察到的内容被视为一项功能!;-)。祝你好运,祝你好运。对于大文件的效率,我建议
'2p;3q'
2{p;q;}
或类似产品。