运行 awk 脚本时出现语法错误,该脚本是:
awk -F\" 'for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}'
实际数据的示例如下(封闭和管道分隔):
"data"|"data"|"data"|"data"|"data"
正如您从代码中看到的那样,我想要做的是:读取所有字段,如果字段是数字(始终是十进制),则用逗号替换该点。我还尝试在 gsub 函数中转义逗号:
awk -F\" 'for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,\,,$1) }}'
但它并没有解决问题,你知道问题出在哪里吗?与正则表达式的比较看起来很好,循环和 if 语句也是,错误消息是
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}
awk: cmd. line:1: ^ 1 is invalid as number of arguments for gsub