LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-05-2011, 09:41 PM   #1
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Rep: Reputation: 51
Talking Question regarding sed.


Hi,

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.
 
Old 09-06-2011, 03:08 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.

See here:
http://mywiki.wooledge.org/BashFAQ/001

Also, be sure to quote all your variables, for the same reason:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

And:
$(..) is highly recommended over `..`.

( ...backticks...we hates them my precious...oh yes we does... )
 
1 members found this post helpful.
Old 09-06-2011, 05:35 PM   #3
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
Hi,

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.
 
Old 09-07-2011, 12:59 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,633

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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.
 
1 members found this post helpful.
Old 09-07-2011, 01:15 AM   #5
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
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?
 
Old 09-07-2011, 04:56 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
1 members found this post helpful.
  


Reply

Tags
sed bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED Question ktr944 Linux - Newbie 4 11-02-2009 10:49 PM
sed Question unihiekka Linux - Newbie 3 09-25-2009 01:08 PM
Another SED question 3saul Linux - Software 2 01-28-2007 01:06 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed question provkitir Programming 4 05-03-2005 10:41 AM


All times are GMT -5. The time now is 08:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration