0

I want to replace <lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/> with null in multiple files.

The code is given below.

sed -i s|<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>||g *

Here I am getting this error:

< was unexpected at this time.

Please clarify for me what is not working here.

tripleee
  • 175,061
  • 34
  • 275
  • 318
U-571
  • 519
  • 2
  • 7
  • 23
  • 1
    You need to use forward slashes, not pipes, and enclose the sed operation in quotes...and escape the existing forward slashes in the search string with backslashes. – mjsqu Jul 24 '18 at 05:31
  • See: https://stackoverflow.com/questions/51490729/gnu-sed-remove-portion-of-line-after-pattern-match-with-special-characters#comment89950143_51490729 – Cyrus Jul 24 '18 at 05:32
  • 1
    @mjsqu The pipes are fine, `sed` specifically allows you to use any nonalphanumeric character as separator in substitutions precisely so that you can use unescaped slashes in the pattern if you want to. – tripleee Jul 24 '18 at 15:36

3 Answers3

3

Could you please try following and let me know if this helps you. By using # as sed's separator you need not to escape / in it only need to escape ., ? not to take their special meaning

sed -E 's#<lexicon uri="file://C:/image/png/grammars/custom/image-custom\.lex\?SWI\.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom\.lex\?SWI\.type=backup"/>##' Input_file

Tested it with:

sed --version
GNU sed version 4.2.1
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
3

works with #
sed -i -e 's#<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>##g' test.txt

rkrankus
  • 99
  • 2
1

The pattern contains shell metacharacters, which need to be quoted or escaped. Usually, in Bash, you should use single quotes around strings, unless you need the shell to interpolate variables and command substitutions and interpret backslash sequences (in which case use double quotes) or to also perform whitespace tokenization and wildcard expansion (in which case use no quotes). See also When to wrap quotes around a shell variable?

sed -i 's|<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>||' *

I also took out the g flag, which only makes sense if you expect multiple matches within a single line. (Perhaps you do after all, in which case obviously put it back.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • If you are not in fact using Bash after all, use the quoting mechanism of your shell. The traditional Windows `cmd` needs double quotes, I believe; though then your question is still severely mis-tagged. – tripleee Jul 24 '18 at 15:45