grep -l \'texttofind\' * | xargs sed -i 's/toreplace/replacewith/g'
Im getting this error when I run the above command in the terminal.
sed: 1: "forkliftDailyChecklistW ...": invalid command code f
I've looked at several forums and have found nothing about code f.
Any help/insight would be appreciated
I figured out what was wrong. I needed to add '' after the -i and before the 's/../../':
grep -l \'texttofind\' * | xargs sed -i '' 's/toreplace/replacewith/g'
Related
I am trying to use sed to replace the following but not working
replace datetime.now(pytz.utc) with datetime.utcnow() recursively
i have tried the following
grep -rl "datetime.now(pytz.utc)" . | xargs sed -i 's/datetime.now\(pytz.utc\)/datetime.utcnow\(\)/g'
mac command equivalent
LC_ALL=C
grep -e "datetime.now(pytz.utc)" -rl . | xargs sed -i '' 's/datetime.now\(pytz.utc\)/datetime.utcnow\(\)/g'
as you can see i tried to escape all the parentheses but does not work
anyone know how to properly use sed to replace datetime.now(pytz.utc) with datetime.utcnow()?
I tried to explain in the comments, but obviously I wasn't clear. Here are two potential solutions to your problem:
Using your 'grep/xargs' method:
grep -rl "datetime.now(pytz.utc)" . | xargs sed -i 's/datetime.now(pytz.utc)/datetime.utcnow()/g'
Using the 'find/exec' method:
find . -type f -exec sed -i 's/datetime.now(pytz.utc)/datetime.utcnow()/g' {} \;
Both options will replace "datetime.now(pytz.utc)" with "datetime.utcnow()" in the files found. Both answers are platform independent provided you have GNU sed, not BSD sed.
inside minor1.sed
sed -r 's/8/1/g' phone.txt
inside phone.txt
(866) 879-7647
(888) 474-7424
(371) 670-6006
(866) 266-5588
(844) 415-3955
(800) 237-2747
command issued
sed -r -f minor1.sed phone.txt
no matter what I do im given the unterminated 's command error.
minor1.sed should contain only sed expression, s/8/1/g in your case:
val#chi:~$ cat minor1.sed
s/8/1/g
val#chi:~$ sed -r -f minor1.sed phone.txt
(166) 179-7647
(111) 474-7424
(371) 670-6006
(166) 266-5511
(144) 415-3955
(100) 237-2747
I need regexp which would remove tinrab in all directories
grep -rl "tinrab" /home/miki/go/src/github.com/meower
/home/miki/go/src/github.com/meower/pusher-service/main.go
/home/miki/go/src/github.com/meower/Dockerfile
/home/miki/go/src/github.com/meower/event/nats.go
/home/miki/go/src/github.com/meower/event/event.go
/home/miki/go/src/github.com/meower/meow-service/handlers.go
/home/miki/go/src/github.com/meower/meow-service/main.go
/home/miki/go/src/github.com/meower/query-service/handlers.go
/home/miki/go/src/github.com/meower/query-service/main.go
/home/miki/go/src/github.com/meower/search/elastic.go
/home/miki/go/src/github.com/meower/search/repository.go
/home/miki/go/src/github.com/meower/Gopkg.lock
/home/miki/go/src/github.com/meower/Gopkg.toml
/home/miki/go/src/github.com/meower/db/repository.go
/home/miki/go/src/github.com/meower/db/postgres.go
/home/miki/go/src/github.com/meower/.git/logs/HEAD
/home/miki/go/src/github.com/meower/.git/logs/refs/heads/master
/home/miki/go/src/github.com/meower/.git/logs/refs/remotes/origin/HEAD
/home/miki/go/src/github.com/meower/.git/config
For example in Dockerfile I have this line
WORKDIR /go/src/github.com/tinrab/meower
My goal is to have line without tinrab
WORKDIR /go/src/github.com/meower
This regexp
replaceAll("/tinrab*/ *", " ")
should somehow be implemented.
Any ideas?
If I try
grep -rl "tinrab" /home/miki/go/src/github.com/meower | xargs replaceAll("\/tinrab\/", "")
I got
bash: syntax error near unexpected token `('
You said you wanted a regexp /tinrab*/ *, but based on the examples you gave I guess /tinrab will do what you want..
grep -rl "tinrab" /home/miki/go/src/github.com/meower | \
xargs -r sed -i 's!/tinrab!!g'
The general format of the sed expression I used is: s!<from>!<to>!g which works the same as the replaceAll("<from>", "<to>") pseudocode you considered.
I want to replace one string with another but I can't. The code is:
updatedb
MCRYPTINI=$(locate mcrypt.ini | grep 'apache2')
MCRYPTSO=$(locate mcrypt.so | grep "/mcrypt.so")
OLD="extension=mcrypt.so"
NEW="extension=$MCRYPTSO"
echo $MCRYPTINI
echo $MCRYPTSO
echo $OLD
echo $NEW
echo "'s/$OLD/$NEW' $MCRYPTINI"
sed -i 's/$OLD/$NEW' $MCRYPTINI
And the result is:
sudo sh testScript.sh
/etc/php5/apache2/conf.d/20-mcrypt.ini
/usr/lib/php5/20121212/mcrypt.so
extension=mcrypt.so
extension=/usr/lib/php5/20121212/mcrypt.so
's/extension=mcrypt.so/extension=/usr/lib/php5/20121212/mcrypt.so' /etc/php5/apache2/conf.d/20-mcrypt.ini
sed: -e expression #1, char 11: unterminated `s' command
For the response I don't need to use 'sed', but it's looks easy and good.
I use sh not bash because I want the code can use in all the systems, so I prefer answers that follow that principle
UPDATE
sed -i "s/$OLD/$NEW/" $MCRYPTINI
error:
sed: -e expression #1, char 14: unknown option to `s'
Add a slash and double quotes:
sed -i "s/$OLD/$NEW/" file
The solution could be:
sed -i "s/$OLD/$NEW/" $MCRYPTINI
but $NEW is a path, so I need to change "/" by other character, for example "+"
sed -i "s+$OLD+$NEW+" $MCRYPTINI
I have the following extract from a script that fetches weather information from accuweather:
wget -O ./weather_raw $address
if [[ -s ./weather_raw ]]; then
egrep 'Currently|Forecast<\/title>|_31x31.gif' ./weather_raw > ./weather
sed -i '/AccuWeather\|Currently in/d' ./weather
sed -i -e 's/^[ \t]*//g' -e 's/<title>\|<\/title>\|<description>\|<\/description>//g' ./weather
sed -i -e 's/<img src="/\n/g' ./weather
sed -i '/^$/d' ./weather
sed -i -e 's/_31x31.*$//g' -e 's/^.*\/icons\///g' ./weather
sed -i -e '1s/.$//' -e '3s/.$//' -e '6s/.$//' ./weather
for (( i=2; i<=8; i+=3 ))
do
im=$(sed -n ${i}p ./weather)
sed -i $i"s/^.*$/$(test_image $im)/" ./weather
done
fi
The command that triggers the code above is in a conkyrc file and its ~/.conkyblue/accu_weather/rss/acc_rss. When I run the conkyrc script from the prompt, I get an error
sed: can't read /home/me/weather: No such file or directory
And indeed when I check, the "weather" file is not created. However if run the command ~/.conkyblue/accu_weather/rss/acc_rss directly from the prompt, it works as expected and create and puts content into the /home/me/weather file.
I don't know anything about the sed command although I'm trying to learn it as a result of this bother.
What could be the problem with the code. I don't think its a permission issue since the folder its writing into is my home folder and I of-course own it.
Thanks
It should have been created by egrep.
When you run your script, the weather directory will be created in the pwd of the script process.
Check and see why egrep does not create the file, or in which directory it created it.