0

I have below txt

604024692;-27.34
500570067;-.0835
604013284;0.00006
603839102;-.1121

I want it to be:

604024692;27.34
500570067;0.0835
604013284;0.00006
603839102;0.1121

but when I run below

sed 's/-/\0/'

it gives me

604024692;027.34
500570067;0.0835
604013284;0.00006
603839102;0.1121

I am using AIX 7.1 Any help is appreciated.

3 Answers 3

3
$ awk 'BEGIN{FS=OFS=";"} {$2=($2<0?-$2:$2)}1' file
604024692;27.34
500570067;0.0835
604013284;0.00006
603839102;0.1121
Sign up to request clarification or add additional context in comments.

Comments

1

This one-liner may help:

awk -F';' -v OFS=";" '{sub(/^-/,"0",$2);$2+=0}7' file

The idea is, after substitution, we let awk handle the leading zero.

With your data as example:

kent$  cat f
604024692;-27.34
500570067;-.0835

kent$  awk -F';' -v OFS=";" '{sub(/^-/,"0",$2);$2+=0}7' f
604024692;27.34
500570067;0.0835

3 Comments

Thanks a lot. it worked but introduce another issue. I have 604013284;0.00006 in my file and this was turned to 604013284;6e-05
when you ask a question here, please list all your requirements in the question, do NOT put them in comments.
@Drsin and what version of awk do you use? here with gawk, 0.00006 was changed into 6e-05 automatically. Anyway, you can output $2 with printf "%g",$2 to achieve it. Do some test you will see.
0

You could try the below sed command.

$ sed 's/-//g;s/\(^\|;\)\./\10./g' file
604024692;27.34
500570067;0.0835

s/-//g, removes all the minus symbols. s/\(^\|;\)\./\10. matches the dot which are at the start or the dot which are next to the semicolon and replaces the match with the chars inside the group index 1 plus the digit 0 plus a dot.

2 Comments

@Raj, Thanks. Yours also work but created another issue. I have updated my initial post. See above.
@Drsin what do you mean? my code works for the above updated input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.