Replacing character position within the string of a file
Hi,
I am trying to read a file that has a double value followed by minus sign (Such as 9999999.99-). I want to take this minus from end of the number to beginning of the number (such as -9999999.99). Could you please help me how can achieve this? File is delimited by coma and format has provided below - asdfasdf,7887687.88-,aasdfas 213412.78-,5345645.00,shdfjghh dsafsdf,asdfafdf,23452345.78- Thanks a lot. |
Hi,
Would this help: sed 's/\([0-9][0-9]*\.[0-9][0-9]\)-/-\1/' infile Sample run on given example: Quote:
Part of the search string (the number part) is between \( and \), all that is found can be represented as \1 in the replace string. This is called back referencing. Hope this helps. |
Quote:
Thanks for the quick respnose but it is not working for me. I am using korn shell. does it matter? Here is the output - sed 's/\([0-9][0-9]*\.[0-9][0-9]\)-/-\1/' test asdfas,98789789.90,-7868876.90 -1234.88,asdfas,123412341.66 asdfasd,-2452345.45.23452345.90 -34523.88,3245.77,23452345- Please advice. Thanks |
Code:
sed -r 's/([[:digit:]]+\.[[:digit:]][[:digit:]])-/-\1/g' junk I did try this version will an entry that had two numbers followed by a minus sign. My input set and sed command assumes that there are two digits after the decimal point. Code:
The only real difference between the two sed commands is that I added "g" at the end to handle the case of more than one number being changed on a single line. We both assumed two digits after the decimal point. You could use this version is the decimal point is optional. It still assumes two digits after the decimal point. You can use [[:digit:]]{1,} or something similar instead. Code:
sed -r 's/([[:digit:]]+)(\.[[:digit:]][[:digit:]])?-/-\1\2/g' junk Another possible solution is to change your locale setting so that your data set doesn't print numbers that way (if you are producing them yourself). |
Hi,
You don't show what your input file looks like, but the output does look correct. Maybe you think the last number on the last line is wrong, but it isn't (theres no dot followed by 2 numbers). |
I did a cut and paste from your results and got the same (right answer) as Druna. (I'm on BASH)
The results that you are getting suggest that the ranges (eg [0-9]) are not being interpreted correctly. You might want to try sed -r (the -r flag turns on extended regular expressions.) Also, try experiments with simpler expressions, eg: sed 's/[0-9]/XXX/' filename (This should replace only the first occurence of any numeral.) Note that you probably want the g flag unless you know that you will only have one match per line. eg: sed 's/[0-9]/XXX/g' filename (replaces ALL occurences of a numeral.) Why this would be different in the KORN shell is way above my pay grade... |
Hi,
pixellany is completely correct about adding the g if multiple numbers (with dots and 2 numbers) are on one line. |
Hi,
I am getting the following error message. Thanks sed: Not a recognized flag: r Usage: sed [-n] Script [File ...] sed [-n] [-e Script] ... [-f Script_file] ... [File ...] |
Hi,
Without knowing what you are doing we cannot help you :) Please post the command(s) you tried, the inputfile you use and the output (if any). |
Hi Here is all information.
Thanks Here is the content of my file called test: asdfas,98789789.90,7868876.90- 1234.88-,asdfas,123412341.66 asdfasd,2452345.45-.23452345.90 34523.88-,3245.77,23452345- And this is the command I did use: sed -r 's/([[:digit:]]+\.[[:digit:]][[:digit:]])-/-\1/g' test And this is the error message I got: sed: Not a recognized flag: r Usage: sed [-n] Script [File ...] sed [-n] [-e Script] ... [-f Script_file] ... [File ...] |
Hi,
The -r flag is not needed. Both jschiwal's and my solution (with the g included) give the correct output: Quote:
|
Hi,
Thanks a lot. It is working now. What I need to do if I want ignore the decimal restriction? I mean, I want to make changes on the number that does not have decimal. Such as the last number in last row. Thanks |
Hi,
Is this better: sed 's/\([0-9][0-9\.]*[0-9]\)-/-\1/g' infile? Example run: Quote:
|
Quote:
Thanks a lot. |
All times are GMT -5. The time now is 08:11 PM. |