获取 STDOUT 第 n 行的命令

发布于 2024-08-05 00:59:33 字数 467 浏览 8 评论 0原文

是否有任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

挽你眉间 2024-08-12 00:59:33

使用 sed,只是为了多样化:

ls -l | sed -n 2p

使用这种替代方案,它看起来更有效,因为它在打印所需的行时停止读取输入,可能会在馈送过程中生成一个 SIGPIPE,进而生成一个不需要的错误消息:

ls -l | sed -n -e '2{p;q}'

我经常看到这种情况,所以我通常使用第一个(无论如何,它更容易输入),尽管 ls 不是一个在收到 SIGPIPE 时抱怨的命令。

对于一系列行:

ls -l | sed -n 2,4p

对于多个行范围:

ls -l | sed -n -e 2,4p -e 20,30p
ls -l | sed -n -e '2,4p;20,30p'

Using sed, just for variety:

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:

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.

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'
我最亲爱的 2024-08-12 00:59:33
ls -l | head -2 | tail -1
ls -l | head -2 | tail -1
泼猴你往哪里跑 2024-08-12 00:59:33

漂亮的头/尾方式的替代方案:

ls -al | awk 'NR==2'

ls -al | sed -n '2p'

Alternative to the nice head / tail way:

ls -al | awk 'NR==2'

or

ls -al | sed -n '2p'
阪姬 2024-08-12 00:59:33

来自sed1line

# print line number 52
sed -n '52p'                 # method 1
sed '52!d'                   # method 2
sed '52q;d'                  # method 3, efficient on large files

来自awk1line

# print line number 52
awk 'NR==52'
awk 'NR==52 {print;exit}'          # more efficient on large files

From sed1line:

# print line number 52
sed -n '52p'                 # method 1
sed '52!d'                   # method 2
sed '52q;d'                  # method 3, efficient on large files

From awk1line:

# print line number 52
awk 'NR==52'
awk 'NR==52 {print;exit}'          # more efficient on large files
朮生 2024-08-12 00:59:33

为了完整起见;-)

代码越短,

find / | awk NR==3

寿命越短

find / | awk 'NR==3 {print $0; exit}'

For the sake of completeness ;-)

shorter code

find / | awk NR==3

shorter life

find / | awk 'NR==3 {print $0; exit}'
迷荒 2024-08-12 00:59:33

试试这个 sed 版本:

ls -l | sed '2 ! d'

它说“删除所有不是第二行的行”。

Try this sed version:

ls -l | sed '2 ! d'

It says "delete all the lines that aren't the second one".

疾风者 2024-08-12 00:59:33

另一位海报建议,

ls -l | head -2 | tail -1

但如果你将 head 插入 tail,看起来 N 行之前的所有内容都会被处理两次。

将尾巴插入头部

ls -l | tail -n +2 | head -n1

会更有效吗?

Another poster suggested

ls -l | head -2 | tail -1

but if you pipe head into tail, it looks like everything up to line N is processed twice.

Piping tail into head

ls -l | tail -n +2 | head -n1

would be more efficient?

浊酒尽余欢 2024-08-12 00:59:33

您可以使用 awk:

ls -l | awk 'NR==2'

更新

上面的代码不会得到我们想要的结果,因为差一错误:ls -l 命令的第一行是 total 行。为此,以下修改后的代码将起作用:

ls -l | awk 'NR==3'

You can use awk:

ls -l | awk 'NR==2'

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:

ls -l | awk 'NR==3'
月亮坠入山谷 2024-08-12 00:59:33

Perl 对你来说容易使用吗?

$ perl -n -e 'if ($. == 7) { print; exit(0); }'

显然用任何你想要的数字代替 7。

Is Perl easily available to you?

$ perl -n -e 'if ($. == 7) { print; exit(0); }'

Obviously substitute whatever number you want for 7.

久隐师 2024-08-12 00:59:33

是的,最有效的方法(正如 Jonathan Leffler 已经指出的那样)是将 sed 与 print & 一起使用。辞职:

set -o pipefail                        # cf. help set
time -p ls -l | sed -n -e '2{p;q;}'    # only print the second line & quit (on Mac OS X)
echo "$?: ${PIPESTATUS[*]}"            # cf. man bash | less -p 'PIPESTATUS'

Yes, the most efficient way (as already pointed out by Jonathan Leffler) is to use sed with print & quit:

set -o pipefail                        # cf. help set
time -p ls -l | sed -n -e '2{p;q;}'    # only print the second line & quit (on Mac OS X)
echo "$?: ${PIPESTATUS[*]}"            # cf. man bash | less -p 'PIPESTATUS'
零崎曲识 2024-08-12 00:59:33

sed 在我的情况下不起作用。
我建议:

对于“奇数”行 1,3,5,7... ls |awk '0 == (NR+1) % 2'

对于“偶数”行 2,4,6,8 ls |awk '0 == (NR) % 2'

Hmm

sed did not work in my case.
I propose:

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'

入画浅相思 2024-08-12 00:59:33

为了更加完整......

ls -l | (for ((x=0;x<2;x++)) ; do read ; done ; head -n1)

丢弃行直到到达第二行,然后打印出第一行。所以,它打印第三行。

如果只是第二行..

ls -l | (read; head -n1)

根据需要放置尽可能多的“读取”。

For more completeness..

ls -l | (for ((x=0;x<2;x++)) ; do read ; done ; head -n1)

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..

ls -l | (read; head -n1)

Put as many 'read's as necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文