获取 STDOUT 第 n 行的命令
是否有任何 bash 命令可以让你获取 STDOUT 的第 n 行?
也就是说,如果
$ ls -l
-rw-r--r--@ 1 root wheel my.txt
-rw-r--r--@ 1 root wheel files.txt
-rw-r--r--@ 1 root wheel here.txt
我意识到在编写要重用的脚本
$ ls -l | magic-command 2
-rw-r--r--@ 1 root wheel files.txt
时这将是不好的做法,但是在日常使用 shell 时,能够过滤对我来说很有用我的 STDOUT 以这种方式。
我还意识到这将是一个半简单的命令(缓冲 STDOUT,返回特定行),但我想知道是否有一些标准 shell 命令可以做到这一点,而无需我放弃脚本到位。
Is there any bash command that will let you get the nth line of STDOUT?
That is to say, something that would take this
$ ls -l
-rw-r--r--@ 1 root wheel my.txt
-rw-r--r--@ 1 root wheel files.txt
-rw-r--r--@ 1 root wheel here.txt
and do something like
$ ls -l | magic-command 2
-rw-r--r--@ 1 root wheel files.txt
I realize this would be bad practice when writing scripts meant to be reused, BUT when working with the shell day to day it'd be useful to me to be able to filter my STDOUT in such a way.
I also realize this would be semi-trivial command to write (buffer STDOUT, return a specific line), but I want to know if there's some standard shell command to do this that would be available without me dropping a script into place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用 sed,只是为了多样化:
使用这种替代方案,它看起来更有效,因为它在打印所需的行时停止读取输入,可能会在馈送过程中生成一个 SIGPIPE,进而生成一个不需要的错误消息:
我经常看到这种情况,所以我通常使用第一个(无论如何,它更容易输入),尽管
ls
不是一个在收到 SIGPIPE 时抱怨的命令。对于一系列行:
对于多个行范围:
Using
sed
, just for variety:Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message:
I've seen that often enough that I usually use the first (which is easier to type, anyway), though
ls
is not a command that complains when it gets SIGPIPE.For a range of lines:
For several ranges of lines:
漂亮的头/尾方式的替代方案:
或
Alternative to the nice head / tail way:
or
来自sed1line:
来自awk1line:
From sed1line:
From awk1line:
为了完整起见;-)
代码越短,
寿命越短
For the sake of completeness ;-)
shorter code
shorter life
试试这个 sed 版本:
它说“删除所有不是第二行的行”。
Try this
sed
version:It says "delete all the lines that aren't the second one".
另一位海报建议,
但如果你将 head 插入 tail,看起来 N 行之前的所有内容都会被处理两次。
将尾巴插入头部
会更有效吗?
Another poster suggested
but if you pipe head into tail, it looks like everything up to line N is processed twice.
Piping tail into head
would be more efficient?
您可以使用 awk:
更新
上面的代码不会得到我们想要的结果,因为差一错误:ls -l 命令的第一行是 total 行。为此,以下修改后的代码将起作用:
You can use awk:
Update
The above code will not get what we want because of off-by-one error: the ls -l command's first line is the total line. For that, the following revised code will work:
Perl 对你来说容易使用吗?
显然用任何你想要的数字代替 7。
Is Perl easily available to you?
Obviously substitute whatever number you want for 7.
是的,最有效的方法(正如 Jonathan Leffler 已经指出的那样)是将 sed 与 print & 一起使用。辞职:
Yes, the most efficient way (as already pointed out by Jonathan Leffler) is to use sed with print & quit:
嗯
sed 在我的情况下不起作用。
我建议:
Hmm
sed did not work in my case.
I propose:
为了更加完整......
丢弃行直到到达第二行,然后打印出第一行。所以,它打印第三行。
如果只是第二行..
根据需要放置尽可能多的“读取”。
For more completeness..
Throw away lines until you get to the second, then print out the first line after that. So, it prints the 3rd line.
If it's just the second line..
Put as many 'read's as necessary.