Text Processing – Output Negative Values Regardless of Original Sign on Selected Lines

text processing

I am using the following code to read specific lines in an ASCII file "text.dat"

sed -n '/Columnaxis/p' text.dat

The output is

Columnaxis: 100

In order to output only the second part previous line, I added the following to the previous command line:

 sed -n '/Columnaxis/p' text.dat | awk '{ print $2 }'

The output of the last command is "100"

How can I update the last code to output negative numbers, regardless of the number's original sign? ( i.e if the input is "+100" I want the code to output "-100" also if the input is "-100", I want the code to output "-100"

PS. The content of text.dat is something like the following :

Columnaxis: 100
Columnaxis_1: 100
Columnaxis_2: -100
Columnaxis_3: 50
Columnaxis_4: -2.3
Columnaxis_5: 1.2
Columnaxis_6: -5
Columnaxis_7: -2
Columnaxis_8: -4
Columnaxis_9: -2

I want the code to output the following:

Columnaxis: -100

Let's say that the input has "Columnaxis: +100", then I want it to be

Columnaxis: -100

Best Answer

Or awk alone:

$ cat text.dat
Columnaxis: 100
Columnaxis: +100
Columnaxis: -100

$ awk '/Columnaxis/ { gsub("-|+", "", $2); print "-"$2 }' text.dat
-100
-100
-100
Related Question