Help to move leading negative sign to trailing position

Hi All,

I am having a file which contains negative numbers, wht i am doing is re-formattting the file(moving few columns and add few hard codings between), while reformatting i would want the negative numbers to have the sign as trailing rather than leading.

Existing

-2400.00
34
0.00
-987.00
-92.56

Required

2400.00-
34
0.00
987.00-
92.56-

FYI am using awk on every line to reformat the line to create a new format

cat $FILENAME |grep ^3 | while read line
do
  echo "$line" | nawk ' BEGIN {FS=","}  {print("5",$1,$28,"$27",$26)}' >> New_file
done
 
$ nawk '{ if(index($0,"-") > 0){print substr($0,2,length($0))"-"} else {print $0}}' test
2400.00-
34
0.00
987.00-
92.56-

$ cat test
-2400.00
34
0.00
-987.00
-92.56

Alternate awk..

awk -F'-' '{print /-/?$2"-":$0}' inputfile
 
cat $FILENAME |grep ^3 | while read line
do
echo "$line" | nawk ' BEGIN {FS=","} {print("5",$1,$28,"$27",$26)}' >> New_file
done

use this... ( unnecessary of cat, while, echo )

nawk ' /^3/ BEGIN {FS=","} {print("5",$1,$28,"$27",$26)}' $FILENAME >> New_file

Thank you ITKAMARAJ..
I am actually going to use printf to use certain formatting(like padding with zero etc)

nawk '{ if(index($0,"-") > 0){print substr($0,2,length($0))"-"} else {print $0}}' test

the above worked perfectly fine ... in that case i will have to first use this to the file(to conver the column with trailing sign and then use my other formatting ? or can i include the above to this statement

nawk ' /^3/ BEGIN {FS=","} {print("5",$1,$28,"$27",$26)}' $FILENAME

?

cat 9temp

-2400.00
34
0.00
-987.00
-92.56

With the help of sed:

cat 9temp | sed 's/^-\(.*\)/\1\-/'
2400.00-
34
0.00
987.00-
92.56-

if the above column is other than 1st ..like if the column containg the value is 13th column ?

To change in any column try:

sed 's/-\([0-9.]*\)/\1-/g' file
awk '$13 < 0{$13=$13*-1"-"}1' file

not tested

 
nawk -F"," ' /^3/ {
{ 
   if(index($13,"-") > 0)
   {
          $13=substr($13,2,length($0))"-"
   }
   print($13,"5",$1,$28,"$27",$26)
}' $FILENAME >> New_file
1 Like

Thank you Franklin

The above stuff worked fine but with a small typo error corrected, just removed the 1 at last :slight_smile:

awk '{print $0<0?-$0"-":$0}' inputfile
1 Like