[英]Find a variable length number inside a string from a file using awk?
我在UNIX上有很多文件,希望在该文件中获取与指定模式相关的编号。
大部分文件将在文件中具有唯一的模式,如下所示
some text abc
some text abc
some text abc
(3 rows)
我只想使用awk打印数字3
。
该数字可能因文件而异,可能是35644或任何数字值。
我想使用awk not sed或grep查找该数字,因为HP UNIX不支持高级sed / grep功能。
以下内容对您有用吗?
sed -e 's/(\([0-9][0-9]*\) rows)/\1/;t;d'
如果(N rows)
被N
替换成功,则t
转到脚本的末尾,否则, d
删除该行(即,将跳过不包含该表达式的所有行)。
由于您不希望(或不能)使用简单的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 2
sed
解决方案(在我的变体中)将如下所示:
for f in file[123]; do echo -n "$f "; sed -n '$ s/[() ]\|rows//gp' "$f"; done
也许与此awk
awk -v RS='(' 'END{print $1}' infile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.