bash 获取第 n 行 STDOUT 的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1429556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command to get nth line of STDOUT
提问by Alan Storm
Is there any bash command that will let you get the nth line of STDOUT?
是否有任何 bash 命令可以让您获得第 n 行 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.
我意识到这在编写打算重用的脚本时是不好的做法,但是在日常使用 shell 时,能够以这种方式过滤我的 STDOUT 对我来说很有用。
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 standardshell command to do this that would be available without me dropping a script into place.
我也意识到这将是编写的半简单命令(缓冲区 STDOUT,返回特定行),但我想知道是否有一些标准的shell 命令可以执行此操作,而无需我将脚本放置到位。
回答by Jonathan Leffler
Using sed
, just for variety:
使用sed
, 只是为了多样性:
ls -l | sed -n 2p
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:
使用这种看起来更有效的替代方法,因为它在打印所需的行时停止读取输入,可能会在送入过程中生成一个 SIGPIPE,这反过来可能会生成不需要的错误消息:
ls -l | sed -n -e '2{p;q}'
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.
我经常看到这种情况,我通常使用第一个(无论如何,它更容易输入),尽管ls
它不是一个在获得 SIGPIPE 时会抱怨的命令。
For a range of lines:
对于一系列行:
ls -l | sed -n 2,4p
For several ranges of lines:
对于几个范围的线:
ls -l | sed -n -e 2,4p -e 20,30p
ls -l | sed -n -e '2,4p;20,30p'
回答by mob
ls -l | head -2 | tail -1
回答by ChristopheD
Alternative to the nice head / tail way:
替代漂亮的头/尾方式:
ls -al | awk 'NR==2'
or
或者
ls -al | sed -n '2p'
回答by Mark Edgar
回答by Michael Krelin - hacker
For the sake of completeness ;-)
为了完整起见;-)
shorter code
较短的代码
find / | awk NR==3
shorter life
更短的寿命
find / | awk 'NR==3 {print ls -l | sed '2 ! d'
; exit}'
回答by Paused until further notice.
Try this sed
version:
试试这个sed
版本:
ls -l | awk 'NR==2'
It says "delete all the lines that aren't the second one".
它说“删除所有不是第二行的行”。
回答by Hai Vu
You can use awk:
您可以使用 awk:
ls -l | awk 'NR==3'
Update
更新
The above code will not get what we want because of off-by-one error: the ls -lcommand's first line is the totalline. For that, the following revised code will work:
由于 off-by-one 错误,上面的代码不会得到我们想要的:ls -l命令的第一行是总行。为此,以下修订后的代码将起作用:
$ perl -n -e 'if ($. == 7) { print; exit(0); }'
回答by catfood
Is Perl easily available to you?
Perl 对您来说容易使用吗?
ls -l | head -2 | tail -1
Obviously substitute whatever number you want for 7.
显然用你想要的任何数字代替 7。
回答by nobody
Another poster suggested
另一张海报建议
ls -l | tail -n +2 | head -n1
but if you pipe head into tail, it looks like everything up to line N is processed twice.
但是如果你把头变成尾,看起来直到第 N 行的所有东西都被处理了两次。
Piping tail into head
将尾巴插入头部
##代码##would be more efficient?
会更有效率吗?
回答by stan
Hmm
唔
sed did not work in my case. I propose:
sed 在我的情况下不起作用。我提议:
for "odd" lines 1,3,5,7... ls |awk '0 == (NR+1) % 2'
for "even" lines 2,4,6,8 ls |awk '0 == (NR) % 2'
对于“奇数”行 1,3,5,7... ls |awk '0 == (NR+1) % 2'
对于“偶数”行 2,4,6,8 ls |awk '0 == (NR) % 2'