正则表达式中的 awk 点与空格不匹配

问题描述 投票:0回答:1

我想打印第一个空格之后的所有内容。例如。给定

hello there
我想打印
hello there
,给定
     what's up
我想打印
what's up

我写这篇文章时完全希望它能起作用:

{ print $0 }
match($0, /[^[:space:]].*$/) {
    print $1
}

我认为正则表达式

/[^[:space:]].*$/
将匹配第一个非空格字符,然后
.*$
将匹配其后面的所有字符。

但是正则表达式似乎只捕获下一个空格:

$ echo hello there | awk -f after_indent.awk
hello there
hello
awk posix
1个回答
0
投票

$1
全局被认为是第一个,它不是比赛结果。您必须使用
substr
才能获得比赛结果:

{ print $0 }
match($0, /[^[:space:]].*$/) {
    print substr($0, RSTART, RLENGTH)
}
© www.soinside.com 2019 - 2024. All rights reserved.