23

This works to replace tom with sam in a file:

sed 's/tom/sam/g' file_1 > file_2

But this does not:

sed 's/*****/sam/g' file_1 > file_2

To replace the special characters ***** with the word sam. I have tried with a slash \* but errors.

3
  • 3
    What command did you try with \* and what the errors you got?
    – cuonglm
    Commented Jun 29, 2015 at 18:27
  • i have tried : sed 's/****/sam/g' test.txt > test2.txt and results in error :sed: bad regex '*****': Invalid preceding regular expression - this does have the blackslash \before the *
    – Rosebud
    Commented Jun 29, 2015 at 20:12
  • 1
    You may try to escape every special character: sed 's/\*\*\*\*\*/sam/g'
    – taliezin
    Commented Jun 29, 2015 at 20:12

2 Answers 2

24

You need to escape the special characters with a backslash \ in front of the special character, e.g.:

sed 's/\*/t/g' test.txt > test2.txt

0
2

You need to escape the special characters with a backslash \ in front of the special character. For your case, escape every special character with backslash \.

eg: **** boy is ****

sed 's/\*\*\*\*/sam/g' filename

ans :

sam boy is sam

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .