0

I would like to replace the [sample]. with space in all my document.Could someone let me know how can I do this.I tried the below command but it doesn't work.thanks.

sed -I 's/[sample]./ /g' filename.txt

due to use of [] and . I am having different output. I really appreciate your help

Barmar
  • 741,623
  • 53
  • 500
  • 612
10raw
  • 506
  • 12
  • 26
  • 1
    Have you tried escaping those spexial characters by putting a backslash before them? – vityavv Jun 21 '18 at 21:57
  • 1
    Possible duplicate of [What special characters must be escaped in regular expressions?](https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions) – Esther Jun 21 '18 at 23:58
  • Possible duplicate of [How to replace paired square brackets with other syntax with sed?](https://stackoverflow.com/q/10646418/608639), [What characters do I need to escape when using sed in a sh script?](https://unix.stackexchange.com/q/32907/56041), [Escape a string for a sed replace pattern](https://stackoverflow.com/q/407523/608639), [Which characters need to be escaped in Bash? How do we know it?](https://stackoverflow.com/q/15783701/608639), [sed with special characters](https://stackoverflow.com/q/5980857/608639), etc. – jww Jun 22 '18 at 04:28

2 Answers2

1

You need to escape the special characters with \. Also, there's no -I option, the option to update the file in place is -i.

sed -i 's/\[sample\]\./ /g' filename.txt
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Looks like you're still missing the backslashes before `[` and `]`. And why are you grepping for `sample` but replacing `VA_REPOSITORY`? – Barmar Jun 22 '18 at 00:49
  • You can use `grep -F` to tell it that the pattern is a fixed string, not a regexp, then you don't need to escape anything there. – Barmar Jun 22 '18 at 00:50
  • You're also missing the filename argument to `grep`: `grep -l -F '[sample].` *.txt | ...` – Barmar Jun 22 '18 at 00:51
0

This is really simple my Friend . lets see I have task. Replace all occurrence of string #%$2jh//238720//31223 with $2//23872031223 in /home/bob/data.txt file.

now the solution is there ever you see special character just replace is with \ just like i do as below.

sed -i 's/#%$2jh//238720//31223/$2//23872031223/g' /home/bob/data.txt