Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"invalid command code ." while trying to use grep and sed to replace string in multiple files

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.

like image 700
jellycola Avatar asked Dec 25 '13 14:12

jellycola


People also ask

How replace multiple files in Linux?

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.


1 Answers

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.

like image 177
anubhava Avatar answered Sep 20 '22 22:09

anubhava