0

I dont find the way to use flag with sed and matching pattern.

I'm trying to us the i flag. But I don't understand how it works.

$ sed -i '' -n '/xxx.xxx@xxx.fr/i d' res.txt
sed: 1: "/xxx.xxx@xxx.fr/i d": command i expects \ followed by text

So I want to match xxx.xxx@xxx.fr And XXX.XXX@XXX.FR

The -i '' is only for --in-file (without cache) the d is for delete.

So how can I use flag and eventually multiple of them ? In the documentation I've found it was that way but it seems not to work at all.

Baldráni
  • 5,332
  • 7
  • 51
  • 79
  • what are you trying to do, replace xxx.xxx@xxx.fr with something in file res.txt? – UltrasoundJelly Oct 26 '16 at 09:02
  • using the -i flag will allow the file to be modified in place, rather than outputting to stdout. – UltrasoundJelly Oct 26 '16 at 09:03
  • GNU sed has `I` flag for [case insensitive match](https://stackoverflow.com/documentation/sed/3120/address-and-address-range/13753/lines-matching-regular-expression-pattern#t=201610260907523706883).. nor sure on OSX.. – Sundeep Oct 26 '16 at 09:08
  • @Sundeep nup, tried it already. – Baldráni Oct 26 '16 at 09:08
  • 1
    the error you get is probably because sed thinks you want to use the `i` command which inserts something before the matched pattern.. GNU sed will accept this `seq 5 | sed '/3/i d'` but OSX needs ``\`` after `i`.. so `seq 5 | sed '/3/i\ d'` – Sundeep Oct 26 '16 at 09:10
  • 2
    MacOS doesn't support case insensitive matching http://stackoverflow.com/questions/4412945/case-insensitive-search-replace-with-sed – UltrasoundJelly Oct 26 '16 at 09:12
  • also, I think you shouldn't be using `-n` option when you need `d` command – Sundeep Oct 26 '16 at 09:14

1 Answers1

2

I would use Perl - its regexes and options are far more orthogonal and consistent than all the sed versions across platforms:

perl -i -ne '/XXX.XXX.fr/i || print' res.txt
  • -i means "in-place" editing
  • -n means execute a loop around input lines like awk or sed
  • -e means execute following script
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432