I search/try to write short script (for if loop) to do grep in file by whole cat output.

Already I tried

find -name 'xmlrpc.php' -execdir bash -c '
if [ -e ".htaccess" ] ; then
if grep -qxf /home/tstepien/Desktop/code/testing/addthat.txt .htaccess
        echo found
        (cat /home/tstepien/Desktop/code/testing/addthat.txt && cat .htaccess) > .htaccess.post && mv .htaccess.post .htaccess
fi' {} \;

but it's "grepping" by all words in addthat.txt

example of addthat.txt

RewriteEngine On
RewriteCond %{REQUEST_URI} /xmlrpc.php [NC]
RewriteRule .* - [F,L]

.htaccess (before using script)

here code

.htaccess (after using script - so if statement will show "found" if it will be runned again and will not redo adding content of addthat.txt)

RewriteEngine On
RewriteCond %{REQUEST_URI} /xmlrpc.php [NC]
RewriteRule .* - [F,L]
here some code

and it means that if in .htaccess there is text which is inside of addthat.txt then the contents of addthat.txt shouldn't be added to .htaccess.

migrated from unix.meta.stackexchange.com Apr 19 at 6:49

This question came from our discussion, support, and feature requests site for users of Linux, FreeBSD and other Un*x-like operating systems.

  • What is block.txt? You don't seem to use that file anywhere. Do you mean addthat.txt? And what exactly are you trying to do? Your command is grepping for the entire contents of addthat.txt. Is that what you really want? Please edit your question and give us an example of both addthat.txt and .htaccess and what you want to happen after running your script. – terdon Apr 19 at 8:25
  • ok I will edit - and yes I had block.txt but then I rearranged code and I forgot about that. – TheTanadu Apr 19 at 8:29
  • @terdon I've edited post to clearify – TheTanadu Apr 19 at 8:42
  • 1
    Thanks for the edit, but I'm still confused about what you want exactly. Do you want to add all lines from addthat.txt if any lines are not already in .htaccess? Or do you only want to add those lines that are missing? Your previous grep command was doing something completely different. And what is the actual problem? I mean, what isn't working the way you want it to work? – terdon Apr 19 at 8:58
  • before edit was bad grep - now after comments it's looks more like I want. I want only pass addthat.txt to beginning of file IF it's not exist in beginning. – TheTanadu Apr 19 at 9:21

The syntax you are looking for is close:

grep -q -e "$(cat addthat.txt)" .htaccess

Someone wrote that answer here but now it's deleted. And it works like charm.

grep -qxf /home/tstepien/Desktop/code/testing/addthat.txt .htaccess

-q for quiet mode (only exit code)

-f searching by provided file

-x for searching by whole lines

  • Yes I removed it because it gives a wrong result. It only tells you if any line in addthat.txt is in .htaccess, not if the whole of addthat.txt in in .htaccess. And if you don't add a-x to match only whole lines a line with a single space in addthat.txt is pretty much guaranteed to trigger match. – xenoid Apr 19 at 7:35
  • yes - adding -X is good idea – TheTanadu Apr 19 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.