tags:

views:

64

answers:

5

Temp file has only the number 22.5 in it.

I use

sed 's/.//' Temp

and I expect 225 but get 2.5

Why?

+3  A: 

Because '.' is a regular expression that matches any character. You want 's/\.//'

Paul Tomblin
+5  A: 

The dot is a special character meaning "match any character".

$ sed s/\\.// temp
225

You would think that you could do sed s/\.// temp, but your shell will escape that single backslash and pass s/.// to sed.. So, you need to put two backslashes to pass a literal backslash to sed, which will properly treat \. as a literal dot. Or, you could quote the command to retain the literal backslash:

$ sed "s/\.//" temp
225

The reason you get 2.5 when you do s/.// is that the dot matches the first character in the file and removes it.

Mark Rushakoff
+1 - well stated sir!
Buggabill
A: 

'.' is special: it matches any single character. So in your case, the sed expression matches the first character on the line. Try escaping it like this:

s/\.//

Ned
It doesn't look like you escaped it here.
Telemachus
Heh. Looks like the edit widget eats backslash. I needed to escape the escape to make it show up :)
Ned
+1  A: 

. is a wildcard character for any character, so the first character is replaced by nothing, then sed is done.

You want sed 's/\.//' Temp. The backslash is used to escape special characters so that they regain their face value.

Svante
A: 

you can also use awk

awk '{sub(".","")}1' temp
ghostdog74