I’m getting a syntax error when running an awk script, the script is:
awk -F\" 'for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,,,$1) }}'
A sample of how the actual data may look like is this (enclosed and pipe separated):
"data"|"data"|"data"|"data"|"data"
What i want to do, as you can see from the code is: read all fields, if a field is numeric (will always be decimal), replace the point with a comma.
I have also tried to escape the comma in the gsub function:
awk -F\" 'for( i = 1 ; i <= NF; i++){if ($i == "^[0-9]+$"){ gsub(.,\,,$1) }}'
But its not solving the issue, do you have any idea where the problem is? the comparison against a regex it looks fine, the loop and the if statements also, the error message is
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
this line may work for you:
test :
the problems in your code are:
{}~, not==for regex matching^[0-9]+$match only numbers, 20.8 is not matched. because there is a dot