Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was trying to edit the output of a file using sed. The contents of file are

cat /tmp/Group_RM
t.p@k.p
t.m@k.k

I was trying to get the output as:

cat /tmp/Group_RM
t_p@k_p
t_m@k_k

I tried the below command but the output looked odd:

cat /tmp/Group_RM | sed 's/./_/g'

Output is:

____ ___
_______

sed is replacing all characters with underscores. When I tried to replace "_" with ".", sed works perfectly.

share|improve this question
1  
Please do not sign your posts, and note that you should indent code with four spaces, or select it and press Ctrl-K or click the {} code button. No need for HTML <br>s. Thanks! –  slhck May 18 '13 at 12:30

2 Answers 2

up vote 6 down vote accepted

You need to escape the . like \.. . is a special metacharacter which matches any single character.

$ sed 's/\./_/g' Group_RM
t_p@k_p
t_m@k_k

On a side note, cat is not needed - sed can deal with files itself.

share|improve this answer
    
thanks a lot it worked perfectly :) –  KJT May 18 '13 at 11:25

The reason is that a dot is a special character in regular expressions. If you mean an actual dot, you need to escape it with \.

Also, you do not need cat, you can give the filename to sed directly. This works:

sed 's/\./_/g' myfile.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.