13

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.

  • 2
    What command did you try with \* and what the errors you got? – cuonglm Jun 29 '15 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 Jun 29 '15 at 20:12
  • 1
    You may try to escape every special character: sed 's/\*\*\*\*\*/sam/g' – taliezin Jun 29 '15 at 20:12
16

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

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

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.