0

This question already has an answer here:

I am running this command on linux shell it works smoothly

sed -i 's/\[-.*-]//g' error .txt

What it does is it removes patterns like [-any text-] along with the brackets and - sign.

but when I try to run it through perl script , it removes only '-' sign.

I tried to use system("sed -i 's/\[-.*-]//g' error.txt");

Please what I am doing wrong?

marked as duplicate by muru, Rui F Ribeiro linux Jun 17 at 10:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • How is it duplicate? – Coolstrike Jun 17 at 10:54
1

Since you use double quotes for the string in perl, the \ in the string gets interpreted by perl.

Use a double backslash, i.e. system("sed -i 's/\\[-.*-]//g' error.txt");

Or use [[] instead of \[, i.e. system("sed -i 's/[[]-.*-]//g' error.txt");
This variant will work in the shell as well.

  • Thanks for this similarly will this system("sed -i 's/[{}+.*+]//g' error.txt"); remove this {+anytext+} ? – Coolstrike Jun 17 at 10:25
  • @Coolstrike No. The pattern [{}+.*+] matches any character between the brackets, i.e. one of {, }, +, ., *. The trailing /g makes sed replace all occurrences. Use s/{[+].*[+]}//g – Bodo Jun 17 at 11:22
  • Hi, if I only want to remove [- and -] will this work ? sed -i 's/[-//' file.txt – Coolstrike Jun 20 at 6:37
  • @Coolstrike Please don't add more variants of your question in comments. You should edit your question instead or write a new question. To make your additional question I suggest to add an example of the input text and the expected output. If you want to know if your proposed command works, you can simply try it. (It will not work.) – Bodo Jul 2 at 9:07
0

backslash inside a double quoted string in perl is interpreted by the perl, so the command that is being run is

 sed -i 's/[-.*-]//g' error.txt

and this will remove minus, dot and asterisk characters. The fact that you list minus a second time doesn't worry sed.

perl has a number of quoting tools, in particular you can use the q operator so

 system(q(sed -i 's/\[-.*-]//g' error.txt));

would work. See man perlop

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