[英]extracting a text with awk
I want to grep a file and extract the third part of this line 我想grep文件并提取该行的第三部分
#define SIM_VERSION_COMPAT 1302
with awk. 与awk。 So I wrote:
所以我写道:
grep "#define SIM_VERSION_COMPAT" global.h | awk '{ print $$3 }'
The result should be 1302 but I get nothing (blank). 结果应该是1302,但我什么也没有(空白)。
无需使用grep和管道,您可以像这样使用awk:
awk '/#define SIM_VERSION_COMPAT/{print $3}' global.h
Just using grep
: 仅使用
grep
:
$ grep -Po '(?<=#define SIM_VERSION_COMPAT )[0-9]+' global.h
1302
This uses positive lookbehind to match lines containing #define SIM_VERSION_COMPAT
but only prints the digit string following. 这使用正向
#define SIM_VERSION_COMPAT
匹配包含#define SIM_VERSION_COMPAT
行,但仅显示#define SIM_VERSION_COMPAT
的数字字符串。
[spatel@tc01 ~]$ echo "#define SIM_VERSION_COMPAT 1302" | awk '{ print $3 }'
1302
您也可以使用cut命令
grep "#define SIM_VERSION_COMPAT" temp.txt | cut -d" " -f 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.