Unix (and SO) newbie here trying to flex my nascent command line chops.
I'm trying to use grep and sed to scan a bunch of files in my current directory and replace all occurrences of the string "192.168.1.1" with the string "192.168.1.0" while leaving the .git folder alone.
I tried the following:
grep -lr --exclude-dir=".git" "192.168.1.1" . | xargs sed -i 's/192.168.1.1/192.168.1.0/g'
and I get the following error:
sed: 1: "./contact.html": invalid command code .
I would really appreciate it if someone can help me figure out what's wrong with my command.
I would also be interested in learning about a more efficient way to do this task, particularly if the command is less verbose and thus easier to remember and as long as it does not involve writing a script in perl/bash/etc, but I'm currently trying to drill down sed and grep and I would still be interested in learning about why this command isn't working even if a working alternative was provided.
s/search/replace/g — this is the substitution command. The s stands for substitute (i.e. replace), the g instructs the command to replace all occurrences.
Change your sed command to:
xargs sed -i.bak 's/192\.168\.1\.1/192\.168\.1\.0/g'
sed on OSX needs a valid file extension with -i
option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With