Removing matching text from multiple files with a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing matching text from multiple files with a shell script
# 1  
Old 05-28-2011
Error Removing matching text from multiple files with a shell script

Hello all,

I am in need of assistance in creating a script that will remove a specified block of text from multiple .htaccess files. (roughly 1000 files)

I am attempting to help with a project to clean up a linux server that has a series of unwanted url rewrites in place, as well as some rules that need to stay in place.

The series of rewrite rules that needs to be removed is the same in each file, and the htaccess files are spread throughout 100's of directories within the server.

I am not all that great with command line scripts but I know enough to navigate the server via shell and implement fairly simple scripts. Unfortunately no on else within the company has any experience within maintaining a linux server, so I was delegated to take the lead on this project.

I believe this task can be accomplished using a combination of find, grep, and sed (or awk) but I could use some help in combining these functions to complete the task.

I was able to output a list of all the files that contain the matching rules to a text file to see how extensive the changes that need to be made are going to be using the following:

find /var/www/html/vhosts -name ".htaccess" -exec grep -l "Text To Match" > output.txt {} \;

This seemed to spit out the requested information in a timely matter, now I need to add sed into the equation to actually modify the files that I found, using either the text file containing the path's to each of these files, or piping the output into sed instead of to the text file.

This is where it gets a little bit complicated and the confusion for me sets in. I figured rather than spend countless hours searching google, I would seek the assistance of some others.. that hopefully are a little more knowledgeable than me at the command line.

Can anyone chime in and explain to me how i can incorporate sed or awk into the equation to modify all of these htaccess files to only remove the block of text I specify, while leaving the remaining text in place.

Any help or direction would be greatly appreciated.

Thanks,
BoxX
# 2  
Old 05-28-2011
Welcome to the forum. Please use code tags when posting any code or output.

To do the deletion, you could do the following:
Code:
sed '/beg/,/end/ d' filename

which will delete all lines between the occurence of 'beg' and occurence of 'end' and print the result on stdout. 'beg' and 'end' can be regular expressions. sed's pattern matching is greedy.
You can delete it from the file (after you tested your command) using sed's -i (in-place editing) switch.
You can use find(1)'s -exec command to pass the filenames to sed, just like you were doing with grep:
Code:
find /var/www/html/vhosts -name ".htaccess" -exec sed -i '/BegRE/,/endRE/ d' {} \;

If you can be more specific with what does your 'block of text' to delete look like, we can help you build the regex to match (the start and end point) as closely as possible.
You could also specify line numbers, if that is constant across the files (doubtful).

Last edited by mirni; 05-28-2011 at 06:23 AM..
This User Gave Thanks to mirni For This Post:
# 3  
Old 05-28-2011
Thanks for the response,

That looks to be exactly what I'm looking for..

and example of the text I'm referring to that needs to be removed is as follows:

Code:
# THISMATCHESINEVERYENTRY
RewriteEngine On
RewriteCond %{REQUEST_METHOD}   ^GET$
RewriteCond %{HTTP_REFERER}     ^(http\:\/\/)?([^\/\?]*\.)?(google\.|yahoo\.|bing\.|msn\.|yandex\.|ask\.|excite\.|altavista\.|netscape\.|aol\.|hotbot\.|goto\.|infoseek\.|mamma\.|alltheweb\.|lycos\.|search\.|metacrawler\.|rambler\.|mail\.|dogpile\.|ya\.|\/search\?).*$   [NC]
RewriteCond %{HTTP_REFERER}     !^.*(q\=cache\:).*$   [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*(bing|Accoona|Ace\sExplorer|Amfibi|Amiga\sOS|apache|appie|AppleSyndication).*$   [NC]
# THISMATCHESINEVERYENTRY

The start and end points are clearly marked in each .htaccess file, so deleting everything from the opening match to the closing match is desired.

There are no special characters in the start or end tags, except # and spaces, but there are alot of special characters in the contained block.

would...
Code:
find /var/www/html/vhosts -name ".htaccess" -exec sed -i '/# THISMATCHESINEVERYENTRY/,/# THISMATCHESINEVERYENTRY/ d' {} \;

suffice?

---------- Post updated at 02:19 AM ---------- Previous update was at 12:50 AM ----------

Code:
find /var/www/html/vhosts -name ".htaccess" -exec sed -i '/# THISMATCHESINEVERYENTRY/,/# THISMATCHESINEVERYENTRY/ d' {} \;

seems to be the winner Smilie

Thanks for the help. breaking it down, that makes perfect sense. And seems to work in the few test runs I've made.

Kudos ^^

-BoxX
# 4  
Old 05-28-2011
It's always good to make the regex as specific as possible. Your command will work, but it would also match a line:
Code:
# this is what we use to separate block: # THISMATCHESINEVERYENTRY

I'd use anchors:
Code:
sed '/^# THISMATCHESINEVERYENTRY *$/,/^# THISMATCHESINEVERYENTRY *$/ d'

would only match a line starting with '# THISMATCHESINEVERYENTRY' and zero or more spaces until end of line.

Metacharacters in the lines being deleted don't matter at all.

I made a mistake about sed's matching being greedy. Although it's true for pattern matching, it's not true for matching ranges of lines. When specifying ranges of lines, sed turns a flag on or off, when it encounters the pattern.
# 5  
Old 05-28-2011
Depending on what you want to filter out, take care about this :
if you use # THISMATCHESINEVERYENTRY as the pattern triggering the start/stop deletion, you should make sure that this pattern appear an even number of time.

Consider the following example and see what append with a odd number of that pattern :

Code:
$ cat tst
blabla
# THISMATCHESINEVERYENTRY
filtered out
filtered out
# THISMATCHESINEVERYENTRY
blabla
# THISMATCHESINEVERYENTRY
filtered out
filtered out
filtered out
tutu
# THISMATCHESINEVERYENTRY
toto
# THISMATCHESINEVERYENTRY
filtered out
titi
filtered out

Code:
$ sed '/# THISMATCHESINEVERYENTRY/,/# THISMATCHESINEVERYENTRY/ d' tst
blabla
blabla
toto
$

As you see, the end of the file will be filtered out.

---------- Post updated at 11:37 AM ---------- Previous update was at 11:33 AM ----------

If you choose the same /startRE/ and /endRE/ pattern, every odd occurence of the pattern will start the deletion, make sure that the file you want to parse fit the format you need (so you will avoid bad surprise of unexpected deletion)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

3. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

4. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

5. Shell Programming and Scripting

Removing files matching a pattern

I am on ubuntu 11.10 using bash scripts I want to remove all files matching a string pattern and I am using the following code find . -name "*$pattern*" -exec rm -f {} \;I have encountered a problem when $pattern is empty. In this case all my files in my current directory were deleted. This... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern... (3 Replies)
Discussion started by: hnux
3 Replies

7. UNIX for Dummies Questions & Answers

Using Shell Script To Loop Program Through Multiple Text Files

Hello, So I have approximately 300 files of raw data (.txt) files that I am using to perform statistical analysis. I have been able to construct a Fortran program that is able to perform my statistical analysis on a file by file basis. However, I now want to be able to loop program through... (19 Replies)
Discussion started by: Jimmyd24
19 Replies

8. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

9. Shell Programming and Scripting

Removing matching text

Hello Everyone! Of course I am rather new to these forums, but I have been browsing the forums for awhile, always has a lot of useful information :) This time however, I can not find the information I need, well, not completely. So here is the problem, I am a linux admin (dealing mostly with... (1 Reply)
Discussion started by: Rhije
1 Replies

10. Shell Programming and Scripting

shell script for matching 2 files

Hi, Would anyone be able to suggest on this shell script query? =) From the contents of the 2 sample files below, if both files' 1st fields matched (ex: XYZ1234=XYZ1234), I want to append the 4th field (semi-colon delimited) from File2 to the end of that line in File1. If no match found, then... (8 Replies)
Discussion started by: gholdbhurg
8 Replies
Login or Register to Ask a Question