1

I have a file that looks like this:

18 29 293.434
12 32 9954.343 
12 343 .
12 45 9493.545 

I want to replace the "." on line 3 with "0" using sed.

I can't figure out how to do this without replacing every decimal place in the file.

Any help would be much appreciated.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223

3 Answers3

4

With GNU sed:

sed 's/\B\.\B/0/g' file

Output:

18 29 293.434
12 32 9954.343 
12 343 0
12 45 9493.545

See: \B: non-word boundary

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    For those wanting a posix compliant solution, perhaps `sed -E 's/(^|[[:space:]])[.]([[:space:]]|$)/\10\2/g' file`? – potong Jul 23 '20 at 06:50
1

Using any awk in any shell on every UNIX box and assuming you want to replace . with 0 wherever it occurs alone in any field rather than just if it's in the last field of the 3rd line of input:

awk '{for (i=1; i<=NF; i++) if ($i == ".") $i=0} 1' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

If I understand well, if the dot is at the end, remove spaces and substitute this dot with 0:

sed '/\.$/{s/ //g;s/\.$/0/}' file
#       ^

$ is an anchor, it means at the end of the line


18 29 293.434
12 32 9954.343
123430
12 45 9493.545
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223