我在UNIX上有许多文件,并希望获取该文件中与指定模式相关联的编号。
大多数文件在文件中都有一个独特的模式,如下所示
some text abc
some text abc
some text abc
(3 rows)我只想使用awk打印数字3。
这个数字可以根据文件的不同而不同,可以是35644,也可以是任何数值。
我想使用awk而不是sed或grep来查找这个数字,因为HP unix不支持高级sed/grep特性。
发布于 2018-01-23 18:30:57
下面的方法对你有效吗?
sed -e 's/(\([0-9][0-9]*\) rows)/\1/;t;d'如果N对(N rows)的替换成功,t将转到脚本末尾,否则,d将删除该行(即将跳过所有不包含该表达式的行)。
发布于 2018-01-23 18:32:46
由于您不想(或不能)使用简单的sed方法,下面是awk解决方案:
示例输入文件:
$ head file[123]
==> file1 <==
some text abc
some text abc
some text abc
(3 rows)
==> file2 <==
some text abc
some text abc
some text abc
some text abc
some text abc
(5 rows)
==> file3 <==
some text abc
some text abc
(2 rows)$ for f in file[123]; do awk 'END{ print FILENAME, substr($1, 2) }' "$f"; done
file1 3
file2 5
file3 2sed解决方案(在我的变体中)将如下所示:
for f in file[123]; do echo -n "$f "; sed -n '$ s/[() ]\|rows//gp' "$f"; done发布于 2018-01-23 21:40:00
也许有了这只鹰
awk -v RS='(' 'END{print $1}' infilehttps://stackoverflow.com/questions/48399237
复制相似问题