linux中怎样从家目录进入awk,如何跳过awk中的目录?

假设我有以下文件和目录结构:

$tree

.

├── a

├── b

└── dir

└── c

1 directory,3 files

也就是说,两个文件a和b与dir dir一起,其中另一个文件c代表.

我想用awk(完全是GNU Awk 4.1.1)处理所有文件,所以我这样做:

$gawk '{print FILENAME; nextfile}' * */*

a

b

awk: cmd. line:1: warning: command line argument `dir' is a directory: skipped

dir/c

一切都很好,但*也扩展到目录目录,awk尝试处理它.

所以我想知道:是否有任何本地方式awk可以检查给定元素是否是一个文件,如果是,跳过它?也就是说,不使用system().

我通过在BEGINFILE中调用外部系统使其工作:

$gawk 'BEGINFILE{print FILENAME; if (system(" [ ! -d " FILENAME " ]")) {print FILENAME,"is a dir,skipping"; nextfile}} ENDFILE{print FILENAME,FNR}' * */*

a

a 10

a.wk

a.wk 3

b

b 10

dir

dir is a dir,skipping

dir/c

dir/c 10

还要注意if(system(“[!-d”FILENAME“]”)){print FILENAME,“是dir,skip”“; nextfile}直观地工作:它应该在true时返回1,但它返回退出代码.

Directories on the command line produce a warning and are skipped (see 07002)

然后链接页面说:

4.11 Directories on the Command Line

According to the POSIX standard,files named on the awk command line

must be text files; it is a fatal error if they are not. Most versions

of awk treat a directory on the command line as a fatal error.

By default,gawk produces a warning for a directory on the command

line,but otherwise ignores it. This makes it easier to use shell

wildcards with your awk program:

06003

If either of the –posix or –traditional options is given,then gawk

reverts to treating a directory on the command line as a fatal error.

See 07003,for a way to treat directories as usable

data from an awk program.

事实上情况就是这样:与之前相同的命令–posix失败:

$gawk --posix 'BEGINFILE{print FILENAME; if (system(" [ ! -d " FILENAME " ]")) {print FILENAME,NR}' * */*

gawk: cmd. line:1: fatal: cannot open file `dir' for reading (Is a directory)

我检查了上面链接的16.7.6阅读目录部分,他们讨论了readdir:

The readdir extension adds an input parser for directories. The usage

is as follows:

@load “readdir”

但我不确定如何调用它以及如何从命令行使用它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值