0

I'm trying to delete a string from a file using the follow sed command:

pattern="$name;$date;$hour"
sed -i.bak "/${pattern}/I d" $BOOKFILE

But the problem is that: On OS X – sed does NOT support case-insensitive matching

I need to delete a string from a file using insensitive case. For example if I insert "a;20161010;11" instead of "A;20161010;11" it must recognize and delete the same string from file.

I can't use script in other languages as perl or TCL etc...

kubi
  • 48,104
  • 19
  • 94
  • 118
sgiraz
  • 141
  • 2
  • 11
  • 2
    Possible duplicate of [Case-insensitive search & replace with sed](http://stackoverflow.com/questions/4412945/case-insensitive-search-replace-with-sed) – Alexander O'Mara Apr 03 '16 at 17:55
  • no it is not duplicate because I can't use script in other languages as perl or TCL etc... because it is an exercise for university – sgiraz Apr 03 '16 at 18:07
  • 1
    I'm afraid the `sed` binary will not change to accommodate your desires. You're probably going to have to use *something* else, or get a `sed` binary that supports this. – Alexander O'Mara Apr 03 '16 at 18:11
  • Using [Homebrew](http://brew.sh), install _GNU_ `sed` with `brew install gnu-sed`; invoke it with `gsed`. – mklement0 Apr 03 '16 at 18:20
  • If you really want to use case-sensitive sed only then maybe you can use sed to make the *pattern* case-insensitive, e.g. `sed -E "s/a|A/[aA]/g"` applied to the pattern will make it case-insensitive for a's. If you only expect lower or upper case and not mixed case you can just use two patterns, one for each. HTH – CRD Apr 03 '16 at 18:49

1 Answers1

0

I Solved!

# Delete the line containing the pattern '$name;$date;$hour' and create/update a file.bak
pattern=$(grep -i "$name;$date;$hour" $BOOKFILE)
sed -i.bak "/${pattern}/ d" $BOOKFILE 

I used grep -i command ( -i = insensitive case) to get the pattern , then I used sed with the correct pattern.

sgiraz
  • 141
  • 2
  • 11