Converting negative number to positive in a file

Hi ALL,

I am having semi column separated file as below. I am having negative values for the records starting with 11095. How can I convert that positive number

I tried this below seems not working

sed 's/ \([0-9.]*\)$/ -\1/;t;s/\(.*\)-/\1/ myfile
myfile
18223;15402;292;;;;;;;13360.18;;;;;;;;;;;;;;;;;;;;;;;;;;
18223;15404;292;;;;;;;537250.91;;;;;;;;;;;;;;;;;;;;;;;;;;
18223;15405;292;;;;;;;3907337.18;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15280;292;;;;;;;-3180799.65;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15280;292;;;;;;;-6.99;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15281;292;;;;;;;-2849107.45;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15281;292;;;;;;;-6548.14;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15282;292;;;;;;;-27638.72;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15282;292;;;;;;;-32744579.66;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15405;292;;;;;;;-55763720.12;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15280;292;;;;;;;8672148.20;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15281;292;;;;;;;252847.13;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15282;292;;;;;;;39879533.02;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15283;292;;;;;;;4690420.03;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15284;292;;;;;;;992923.68;;;;;;;;;;;;;;;;;;;;;;;;;;

Hello arunkumar_mca,

Could you please try following and do let me know if this helps you.

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

Thanks,
R. Singh

1 Like

The negative appears to be in field #10, change that if I cannot count correctly.

awk -F ';'  ' $1==11095 {$10=$10 * -1} {print} ' infile> outfile

Verify outfile then rename it.

1 Like

To add to Jim's approach, you need to specify the OFS as well:

awk -F ';'  -v OFS=';' '....
1 Like

Hmm. Using scrutinizer's change, I got onto a different UNIX real flavor - this works correctly, note the addition of sprintf:

 awk -F ';' -v OFS=";"  ' $1==11095 {$10=sprintf("%0.2f",$10 * -1)} {print} ' myfile
18223;15402;292;;;;;;;13360.18;;;;;;;;;;;;;;;;;;;;;;;;;;
18223;15404;292;;;;;;;537250.91;;;;;;;;;;;;;;;;;;;;;;;;;;
18223;15405;292;;;;;;;3907337.18;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15280;292;;;;;;;3180799.65;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15280;292;;;;;;;6.99;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15281;292;;;;;;;2849107.45;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15281;292;;;;;;;6548.14;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15282;292;;;;;;;27638.72;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15282;292;;;;;;;32744579.66;;;;;;;;;;;;;;;;;;;;;;;;;;
11095;15405;292;;;;;;;55763720.12;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15280;292;;;;;;;8672148.20;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15281;292;;;;;;;252847.13;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15282;292;;;;;;;39879533.02;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15283;292;;;;;;;4690420.03;;;;;;;;;;;;;;;;;;;;;;;;;;
99822;15284;292;;;;;;;992923.68;;;;;;;;;;;;;;;;;;;;;;;;;;

Sorry for the error.

1 Like

If you are sure you don't want ANY negatives, try

awk 'gsub(/-/, _)+1' myfile

or

sed 's/-//g' myfile
2 Likes

There is of course tr .
Assuming the file structure remains the same as is shown.

tr -d "-" < /path/to/infile > /path/to/outfile
1 Like