0

I have this code:

for f in $(find -E . -type f ! -regex ".*(zip|png|jpeg|jar|ear|ttf|jpg|git)") ! -path .git; do echo $'\nFile: '$f; sed "$(( $(wc -l <$f) / 2 ))"',$d' $f; done

And it seems to be working well. The problem is that if I change it to:

for f in $(find -E . -type f ! -regex ".*(zip|png|jpeg|jar|ear|ttf|jpg|git)") ! -path .git; do echo $'\nFile: '$f; sed -i "$(( $(wc -l <$f) / 2 ))"',$d' $f; done

Note the -i option in the sed command. I get this error:

sed: 1: "./some/path/ ...": invalid command code .

danielrvt
  • 10,177
  • 20
  • 80
  • 121
  • Try using `set -x` to see what `sed` command is actually run when it fails. – Aaron Dec 10 '19 at 16:11
  • Where should I use that set instruction? – danielrvt Dec 10 '19 at 16:12
  • 3
    in your shell before you execute your command again. It modifies the shell to tell it to display the commands it run before running them. I expect you'll see all the executed `sed` commands. Use `set +x` to remove the option when you're done (or just close your shell, it doesn't persist) – Aaron Dec 10 '19 at 16:13
  • It just says this: `sed: 1: "./my/path/ ...": invalid command code . + for f in '$(find -E . -type f ! -regex ".*(zip|png|jpeg|jar|ear|ttf|jpg|git)")' '!' -path .git + echo '` – danielrvt Dec 10 '19 at 16:18
  • 2
    Are you on OS X ? If so check [this question](https://stackoverflow.com/questions/19456518/invalid-command-code-despite-escaping-periods-using-sed) out (on OS X `-i` expects an additional parameter and will consume the command if it's not there, leading to the error you get) – Aaron Dec 10 '19 at 16:22
  • Yes, I'm in OSX. will check that question, thanks – danielrvt Dec 10 '19 at 16:24
  • 1
    You're welcome ! I'm voting to close your question as a duplicate of the other then – Aaron Dec 10 '19 at 16:25
  • Does this answer your question? [invalid command code ., despite escaping periods, using sed](https://stackoverflow.com/questions/19456518/invalid-command-code-despite-escaping-periods-using-sed) – Enlico Dec 10 '19 at 18:45

0 Answers0