ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to figure out how to use sed to either add or comment out a the head of a file. Specifically, editing .htaccess across all domains under a given rootdoc. So,:
/publich_html-
./sub1
./sub2
./sub2-b
Now, its easy enough to add to the head of .htaccess using something like:
Code:
for i in `find -maxdepth 2 -type f -name ".htaccess"`;do sed -i ’1{h; r rewrite_rules.txt
D;}2{x; G; }‘ $i;echo "Edited $i to read:";head -9 $i;done
Er, maybe that works. I did another way before. I got lucky and just had to echo like > . Any ways, how would I look from the first line of a file for a search string contained in another file. So, if I need to remove everything in rewrite.txt from the head of all .htaccess recursively under pwd?
This is not home work. I am trying to make things easier on myself. Any way, thanks for any help.
I'm not exactly sure I understand what you're asking. Could you explain a little more clearly what changes need to be made where?
But in any case the snippet you posted above is not good. Never use a for loop with input from find or similar commands. It will break when you hit a name with a space in it. Use a while+read loop instead.
Thanks for taking a look at this for me! I had no idea about using a for vs. while loop. good stuff.
What I am trying to figure out is:
If I have multiple docs spread across multiple sub-directories, with each of the documents possibly being unique, how would a make changes automatically. What I would like to do is have a file that would contain a match string and another file that would contain the replace string. so:
search string COULD be a block rule for .htaccess :
Code:
<FilesMatch "^(index|wp-cron)\.php$">
order deny,allow
deny from all
allow from XXXXXXXXXXXX
allow from XXXXXXXXXXXX
allow from XXXXXXXXXXXX
</FilesMatch>
Now, in the above seach string, ALL conditions would have to be met. The only thing that would not matter be the actual ip addresses. However, the rest would need to be matched. Depending on the case, simply commenting the above out would be fine. However, I am trying to understand the HOW in a general since, rather than for just one case.
I can see using a file for a holder of search strings would be very, very useful. Perhaps, searching php files for malicious snippets, etc.
Any way, my description most likely speaks volumes of what I do not know. Thanks for your help.
I would ask if you would go one step further and provide a before and after picture? ie. show us an example file before the change, the information contained in the match and replace
files and then what the original file looks like after.
So far it sounds like you could use awk, bash or something more advanced like ruby or perl.
I guess that means I got learn something else. I have already been told I should look at using awk|sed kinda thing. Any way, I would show you the end results; however, that would be a per case solution. I want to know how to do it for any file. I mean I could post a starbucks menu before and after the change.
match file has some text:
line 1
line 2
line 3
All the above must be written as block, not a single matches. so, match would need to be if line 1 = #line 1 then if line 2 = #line 2 then if line 3 =#line 3 do ( read from file and either replace / uncomment.
I was told I can cat a file in or tac a file; however, that seems kinda bleh. I mean why make a temp file?
But the thing about text processing is that it's pretty much always a "per-case" thing. You have to know the exact pattern/layout of the text you want to affect, and how you want to alter it, in order to craft a proper solution. That means that the first step is always to define your problem clearly.
That's why grail asked for a representative sample of the input file(s), and an example of what the resulting output should be; concrete examples work much better than vague descriptions when trying to communicate your goals.
As for what you've posted so far, it's still about as clear as mud to me. I'm still not sure even what and how many files you're talking about, much less how they're supposed to be used. As best as I can tell, you want to have a "master" file with patterns of some kind in it, and you want to use those patterns to alter other text files in some way.
But what does "All the above must be written as block, not a single matches" mean, exactly? I'm not sure I understand it, especially since your following description is of three separate nested "per-line" tests. And when you call it the "match file" do you mean it's the master file that holds the pattern(s), or perhaps the target that needs to be altered? Speaking of which, what are the other files, how are they formatted, and how will they be altered?
Most likely what you want can be done with a simple loop of some kind, either using a combination of shell scripting and sed/awk, or even just sed/awk alone. But again, the problem needs to be clearly presented before solutions can be given. Even if you're just looking for general concepts at this point, a couple of examples of what you're thinking about would help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.