Spa*_*ker 6 sed posix regular-expression
我需要在 sed 使用的正则表达式中组合不同的字符类。我需要匹配[:word:]
和 减号-
。看起来怎么样?我所有的尝试都未能尝试或寻找解决方案。
在以下字符串中,我想匹配所有内容,直到第一个空格:
foo-bar |
baz-xyz-123 |
Run Code Online (Sandbox Code Playgroud)
POSIXly:
sed 's/[^[:alnum:]_-]//g'
Run Code Online (Sandbox Code Playgroud)
将删除当前区域设置中所有非字母数字字符,_
并且-
.
$ echo 'foo-bar |' | sed -e 's/[^[:alnum:]_-]//g'
foo-bar
Run Code Online (Sandbox Code Playgroud)
但是如果你想打印第一个空格之前的所有内容:
sed -e 's/^\([^ ]*\) .*/\1/'
Run Code Online (Sandbox Code Playgroud)
或者awk
:
awk '{print $1}'
Run Code Online (Sandbox Code Playgroud)