Gra*_*ace 17 awk regular-expression
我正在尝试使用正则表达式作为awk
. 从我的阅读来看,这似乎是可能的,但我无法正确理解语法。
rpm -qa | awk '{ 'FS == [0-9]' ; print $1 }'
awk: cmd. line:1: { FS
awk: cmd. line:1: ^ unexpected newline or end of string
Run Code Online (Sandbox Code Playgroud)
想法?如果不是显而易见的,目标是获取没有版本号的软件列表。
Kus*_*nda 27
你搞砸了引号和语法。要设置输入字段分隔符,最简单的方法是使用-F
命令行上的选项:
awk -F '[0-9]' '{ print $1 }'
Run Code Online (Sandbox Code Playgroud)
或者
awk -F '[[:digit:]]' '{ print $1 }'
Run Code Online (Sandbox Code Playgroud)
This would use any digit as the input field separator, and then output the first field from each line.
The [0-9]
and [[:digit:]]
expressions are not quite the same, depending on your locale. See "Difference between [0-9], [[:digit:]] and \d".
One could also set FS
in the awk
program itself. This is usually done in a BEGIN
block as it's a one-time initialisation:
awk 'BEGIN { FS = "[0-9]" } { print $1 }'
Run Code Online (Sandbox Code Playgroud)
Note that single quotes can't be used in a single-quoted string in the shell, and that awk
strings always use double quotes.
gle*_*man 14
+1 for Kusalananda's answer. Alternately, the FS variable can be set in the BEGIN block:
awk 'BEGIN {FS="[0-9]"} {print $1}'
Run Code Online (Sandbox Code Playgroud)
Changing FS in a action block won't take effect until the next line is read
$ printf "%s\n" "abc123 def456" "ghi789 jkl0" | awk '{FS="[0-9]"; print $1}'
abc123
ghi
Run Code Online (Sandbox Code Playgroud)
The other errors in the question:
==
is a comparison operator, =
is for variable assignment