Trying to use awk to check values and multiple

I am trying to use AWK to read a file, comma delimited, and check each field to see if it has a suffix of - (dash , minus sign) if so then I want to either move the minus sign the the beginning of the field or take the numeric portion of the field and multiply it by negative 1 to get the field signed properly with the negative sign preceding the number.

Here is a sample of my data

16361.62 , 4785.75 , 458.00 , 765.00 ,011018, 110.50 , 200.00- , 59, 25 , 443041
.72 , 222032.98 , 46325.27 , 47363.74 , 101643.13 , 776121.82, 11686.72 , 3082.1
2
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , 105.50-, 76461.75
, 14153.18 , 364, 153
68187.16 , 22005.05 , 6767.50 , 1607.00 ,09,01,18, 881.00-, , 46872.51 , 14042.4
4 , 274, 113
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , 105.50-, 76461.75
, 14153.18 , 364, 153

Hello ziggy6,

Not exactly sure about your complete requirement but based on your statements only this solution is. Could you please try following and let me know if this helps.

awk -F" , " '{for(i=1;i<=NF;i++){if($i~/-/){sub(/-/,"",$i);$i="-"$i}}} 1' OFS=" , "   Input_file

Thanks,
R. Singh

1 Like

R Singh, Looks like we have a winner. I am not well versed on AWK, only grep and sed. I will study this and see if I can understand it. Thanks for the help!

16361.62 , 4785.75 , 458.00 , 765.00 ,011018, 110.50 , -200.00 , 59, 25 , 443041
.72 , 222032.98 , 46325.27 , 47363.74 , 101643.13 , 776121.82, 11686.72 , 3082.1
2
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , -105.50, 76461.75
, 14153.18 , 364, 153
68187.16 , 22005.05 , 6767.50 , -1607.00 ,09,01,18, 881.00, , 46872.51 , 14042.4
4 , 274, 113
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , -105.50, 76461.75
, 14153.18 , 364, 153

Hello ziggy6,

No worries, following is the explanation of code which may help you to understand awk better.

awk -F" , " '                ##-F means to set field delimiter as " , " space comma space for each line.
{
  for(i=1;i<=NF;i++){        ##Starting a for loop which starts from i=1 to till value of NF whose value will be number of fields and do following.
   if($i~/-/){               ##Checking condition here if value of a field is having -(dash) in it then do following.
     sub(/-/,"",$i);$i="-"$i}##Using sub of awk which will substitute dash with NULL in that specific field AND then adding dash before value of current field.
}}
1                            ##1 will print the current line either edited or non-edited one.
' OFS=" , " Input_file       ##Setting OFS as space comma space here and mentioning Input_file name here too.

Thanks,
R. Singh

R Singh,
I found a problem with part of the data, 1607.00 became -1607.00 and -881.00 became 881.00

You may invoke the multiple delimiters by using awk -F['/*%...] and so in. Basically use square brackets.

awk '{
   c=split($0,a, " *,* *");
   for (i=1;i<=c;i++) if(a ~ /-/) {w=a; sub(/-/,"",a); a="-"a; sub(w, a)}
} 1' Input_file > Output_file
1 Like

That worked perfect rdrtx1. Can you tell me what to add to that so I can write the output to a file?

See update to post #7

1 Like

sed mayhap?

sed 's/\([0-9.]\+\)-/-\1/g' file

EDIT: Unfortunately,my mawk doesn't provide "back references", so the closest approximation would be

awk '{gsub (/[0-9.]+-/, "-&x"); gsub (/-x/, "")} 1' file