打印的文件列表特定数量的行 [英] Print specific number line of files list

查看:93
本文介绍了打印的文件列表特定数量的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好日子所有,

今天,我想知道如何打印不同文件的相同数量的行。

Today, I was wondering how to print the same number line of different files.

传统的方法来打印 的具体行 文件是:

The traditional way to print a specific line of a file is:

# Print line number 52
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3

但是,当我想使用它,如:

But, when I want to use it like:

sed '52q;d' Files*

它只打印开头的第一个文件的行号52:文件

先谢谢您的任何线索那么多了。

Thanks so much in advance for any clue.

推荐答案

您可以用awk行打印52这样的文件列表中:

You can use awk to print line 52 of a list of files like this:

awk 'FNR==52' Files*

内置变量 FNR 是当前文件的当前记录编号。使用默认的记录分隔符,此相对应的行号。当FNR等于52,前pression计算结果为true,这样的awk打印就行了。

The built-in variable FNR is the current record number of the current file. Using the default record separator, this corresponds to the line number. When FNR is equal to 52, the expression evaluates to true so awk prints the line.

这是一个潜在的premature优化,但你可能还想跳到下一个文件,而不是处理52后的所有行。在GNU AWK,这可以通过使用下一文件语句来完成:

It's potentially a premature optimisation but you might also want to skip to the next file rather than processing all lines after the 52nd. In GNU awk, this can be done using the nextfile statement:

awk 'FNR==52 {print; nextfile}' Files*

这篇关于打印的文件列表特定数量的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆