sed - simple question

Hello

My file looks like that =>

12.56 have then 7888778.2566 what 44454.54545
878787.66565 if else 4445.54545455

I want to change all '.' on ',' .
I'm trying to do it with sed but I don't know chow to build regular expression to
change 454.4466 on 454,4466 ?

tr '.' ',' < infile > outfile

sed 's/\./,/g'

Got to put a backslash before "." to indicate an actual dot.

sed 's/\./,/g' - the command change all '.' on ',' in the file , but I want change
only dots in numbers .

sed 's/[0-9]\.[0-9]/,/g' filename
sed -e "s/\([0-9]\)\./\1,/g" -e "s/\.\([0-9]\)/,\1/g" file

This will remove numbers before and after dot

this wont work, as single digit is considered

echo "1234.23" | sed 's/\([0-9]*\)\.\([0-9]*\)/\1,\2/'
$ echo dfd.dfd | sed 's/\([0-9]*\)\.\([0-9]*\)/\1,\2/'
dfd,dfd

Yes.it was a mistake...

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

should be fine

Yeah, catch! :slight_smile:

perl -e ' while (<>) { chomp; if( $_ =~ /(\d+)\.(\d+)/ ) { s/\./,/g; print "$_\n"; } else { print "$_\n" } }' filename

:slight_smile:

What is the need to replace number and ' . ' as number and ' , '
or
' . ' and number as ' , ' and number in a combined expression

just one of the them would do,

sed "s/\([0-9]\)\./\1,/g" a

or

sed "s/\.\([0-9]\)/,\1/g"  a

It wont work in the following cases

$ echo ".34" | sed "s/\([0-9]\)\./\1,/g"
.34
$ echo "23." | sed "s/\.\([0-9]\)/,\1/g"
23.

this is cool! :slight_smile:

It didnt strike for me about the ".34" and "34." kind of numbers

perl -e ' while (<>) { chomp; if( $_ =~ /(\d+)\.(\d+)/ || $_ =~ /\.(\d+)/ || $_ =~ /(\d+)\./ ) { s/\./,/g; print "$_\n"; } else { print "$_\n" } }' filename