Galaxy
Galaxy

Reputation: 863

In place shift in a string

I want to convert below text file:

4100,123.45-,6789.66-
4152,1234.11,6789.11

(And many more records)

To

4100,-123.45,-6789.66
4152,1234.11,6789.11 

Note that the input file have minus sign at the end of the number, I would like to correct it using Linux command.

Please help...

Upvotes: 1

Views: 62

Answers (2)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

A perl:

perl -pi -e 's/(\d+?\.?\d+)-/-\1/g' inputfile

Upvotes: 1

myaut
myaut

Reputation: 11504

Use sed!

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

To do it in-place, use -i option:

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

It uses regular expressions. For this case:

  • s/x/y/g -- substitute any occurence of x with y
  • [0-9.]* -- match any number of digits or dots
  • \(\) -- match group and bind to \1, \2, etc. in replacement expression
  • - -- match minus

So this expression matches any number of digits or dots, grouped to \1, ending with minus and puts minus at before that group.

Upvotes: 1

Related Questions