3

I have a file which contains IP addresses and I want to replace "." with "-" through sed.

I am using below command:

sed 's/./-/g' iplist.txt

after running this command the output which I am getting is -------------, it replaces whole IP address with "-".

2 Answers 2

7

sed uses regular expressions to find text that needs to be changed, and in regular expressions a . means to match any character. Your command is telling sed to change any character to a -.

To fix this, you need to escape the . by putting a \ in front of it, to tell sed to only match on actual periods:

sed 's/\./-/g' iplist.txt
1
  • 5
    or include it within bracket [.] May 19, 2021 at 14:54
1

This is a job for tr(1):

tr . - < infile > outfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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