LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regexp for changing only part of the found string (https://www.linuxquestions.org/questions/programming-9/regexp-for-changing-only-part-of-the-found-string-698654/)

maginotjr 01-20-2009 01:47 PM

Regexp for changing only part of the found string
 
Hi!

I love regex but sometimes I get crazy trying to figure out things... I hope you can help me...

here is the thing, I have to find all the ',' inside a large database dumb, but this ',' are only in this kind of mask:
$ANYNUMBER-ANYQTD,ANYNUMBER-ANYQTD$
Actualy the $ is not the regex end of line special operator. (I had to use something like $ to avoid the " and ' inside text fields that were dumped).
Here is the regex Im trying (Im in vim):

%s/\$[0-9]+[,][0-9]+\$/I DONT KNOW HOW TO CHANGE ONLY THE DOT/g


well... I couldnt understand why the above dont even match any line if I have lines like $0,01$, $100,14$ and want they to became $0.01$, $100.14$ and so on...


thks!

Disillusionist 01-20-2009 02:35 PM

Please post a larger sample of the file that you are trying to modify.

Will there be other commas in the file (that need to stay as commas)?

If not why not just try (within vi)
:1,$ s/,/./g

jstephens84 01-20-2009 03:43 PM

Quote:

Originally Posted by maginotjr (Post 3415304)
Hi!

I love regex but sometimes I get crazy trying to figure out things... I hope you can help me...

here is the thing, I have to find all the ',' inside a large database dumb, but this ',' are only in this kind of mask:
$ANYNUMBER-ANYQTD,ANYNUMBER-ANYQTD$
Actualy the $ is not the regex end of line special operator. (I had to use something like $ to avoid the " and ' inside text fields that were dumped).
Here is the regex Im trying (Im in vim):

%s/\$[0-9]+[,][0-9]+\$/I DONT KNOW HOW TO CHANGE ONLY THE DOT/g


well... I couldnt understand why the above dont even match any line if I have lines like $0,01$, $100,14$ and want they to became $0.01$, $100.14$ and so on...


thks!

Use (1st part of exp) (second part of exp) / $1.$2 / I believe that is what you will want to do. This will break the exp into two delimited. so 1,00 would be $1 = 1 and $2 = 00.

Disillusionist 01-20-2009 04:52 PM

I think you meant, from within vi:
Code:

:1,$ s/\([0-9]\),\([0-9]\)/\1.\2/g
or using sed:
Code:

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

maginotjr 01-21-2009 04:27 AM

Hey, thanks! The SED example worked fine here, but the VI example didnt, althought it worked was not the right way, like describle in the mask and like the way sed did...

but thanks for the both

[ ]'s


All times are GMT -5. The time now is 02:26 AM.