Xargs SED back into same file? - sed

Lets say I hit foo.html with find, i would like to pipe the contents back into foo.html
find . -iregex '.*\(html\|htm\)' -printf '%P\0' | \
xargs -0 sed -Ee "s:(http|https)\://(www.|)${domain}[?/]::g" \
> # to what? {\} ???
Right now it does not know what or where its

Pass the -i flag to sed, then it will modify the input file in place.
find . -iregex '.*html?' -printf '%P\0' | xargs -0 sed -i -Ee "s:(http|https)\://(www.|)${domain}[?/]::g"

Related

Issue with Sed no input file when Xgrep

I am trying to create a script which looks for x days old files that have a specific string in it, it then removes it and logs the file it has changed.
My way is probably not the best way, but I am new to this so looking for some help. I have got to a stage where the script works but it does not log the file name it has worked on
Working script is
find /home/test -mtime +5 -type f ! -size 0 | xargs grep -E -l '(abc_Pswd_[1-9])' | xargs -n1 sed -i '/abc_Pswd_[1-9].*/d'
I am trying to get the file name from 2nd part of the script I have tried few things
find /home/test -mtime +7 -type f ! -size 0 | xargs grep -E -l '(abc.1x.[1-9] )' > /home/test/tst.log| xargs -n1 sed -i '/abc_Pswd_[1-9].*/d'
This works in terms of logging the result but it exits with the error "sed: no input files"

find | xargs sed to substitute string in file name instead of file content

$ echo "file_contents" > filename.txt
$ find . -type f -print0 | xargs -0 sed 's/file//g'
_contents
How do you get sed to treat the filename as a string instead of an input file? What command do I need to get "name.txt" as the output instead?
Drop the xargs and -0's.
find . -type f | sed 's/file//g'
This takes the output of find and sends it as input to sed.

find dir xargs rm output to a file.log

I have a bash script, in the end will find folders with modified timestamps greater than 5 days then pipe it to xargs to rm. This is working fine and to print the command I am using -t option for the xargs as well. But I need this output written to a log file.
so my command line is as follows :
find /tmp/test -type d -mtime +5 -print0 | xargs -t -0 -I {} /bin/rm -rf '{}'
I would like to get the output to know which all folders are deleted to a file named rmdirs.log
I tried redirecting it to a file but like below and it wont work;
find /tmp/test -type d -mtime +5 -print0 | xargs -t -0 -I {} /bin/rm -rf '{}' >> rmdirs.log
Any help would be much appreciated.
I created a test environment with touch. This properly deletes the directory and logs the ones deleted to rmdirs.log file.
touch -t 202201010000 tmp/test/old
touch tmp/test/new
find tmp/test -type d -mtime +5 -print | \
tee -a rmdirs.log | \
tr '\12' '\0' | \
xargs -0 -I {} /bin/rm -rf {}
Use teeto append (-a) to the rmdirs.log file.
Use tr to convert the newlines (\12) to null (\0) for safety.
Finally run xargs to remove the files.

Using Sed and Find with Grep Linux

I am writing a script that will saech for php files that contain a phrase and I would like that phrase replaced with a new one below is my little script but it is not working it searches ok but does not work with the search and replace section
find . -type f -name "*.php" -exec grep -H "define('DB_HOST', 'localhost');" {} \; | xargs sed -i "define('DB_HOST', 'localhost');/define('DB_HOST', '10.0.0.1');/g"
can someone explain to me what i am doing wrong
many thanks
Joe
did you forget the 's/' at the beggining of the sed expression? As in
sed 's/expression1/expression2/g'
You seem to have
sed 'espression1/expression2/g'
Edit
Another thing: You don't need to use xarg here. You can use multiple -exec flags - and it will to each only if all the previous succeeded:
find . -name '*.php' -exec grep 'whatever' {} \; -exec sed -i 's/whatever/you want/g' {} \;
This will work:
find . -type f -name "*.php" -exec grep -l "define('DB_HOST', 'localhost');" {} \; | xargs sed -i "s/define('DB_HOST', 'localhost');/define('DB_HOST', '10.0.0.1');/g"
Corrections
Missing s/ in sed search and replace command
use grep -l instead of grep -H

Replace token in text by the filename

I have a bunch of textfiles that contain the token -filename-. I need to replace it by the real path and filename of the file.
Thats what I have so far:
grep -lr -e '-filename-' *.txt | xargs sed -i
's/-filename-/therealname/g'
Is there a way to replace therealname with the name of the file?
Just do a bit more bash-fu
for x in *.txt; do
sed -i "s/-filename-/$x/g" $x;
done
Of course, the newlines are just for clarity. Feel free to cram that into one line.
like
for f in $(grep...) ; do sed -i "s,-filename-,$f,g" $f ; done
you mean?
With xargs it will be something like this.
grep ... | xargs -I% -n 1 sed -i "s,-filename-,%,g" %
for f in YOUR_FILE_LIST_MASK ; do
sed -i "s:-filename-:${f}" ${f}
done
can do it.