Linux / UNIX Tech Support Forum
This is a discussion on Shell Script To change strings / text in a text file within the Shell scripting forums, part of the Development/Scripting category; Hello Nixcraft Users: I am looking for a shell script or command to automate a process of opening many files ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
Hello Nixcraft Users:
I am looking for a shell script or command to automate a process of opening many files in a directory and changing a string of text. Example: I have a apache web server that uses virtual hosting. There are approximately 2300 vhost entries or files. So in the directory /usr/local/apache/conf/vhosts/ there are 2300 files like this: Code:
<virtualhost *> ServerName example.com ServerAlias www.example.com DocumentRoot /export/home/phpuser/docrootx/kuph DirectoryIndex index.php ServerAdmin user@example.com <Directory /export/home/phpuser/docrootx/kuph> RewriteEngine On RewriteBase / RewriteRule ^(.*)/([0-9]+)$ /pages/$2\.php Options +FollowSymLinks -Indexes </Directory> ErrorDocument 404 /stn_page_server.php ErrorDocument 403 /stn_page_server.php CustomLog logs/thefox969/access_log combined ErrorLog logs/thefox969/error_log </virtualhost> Thanks for your help Jaysunn Last edited by nixcraft; 05-08-2009 at 06:02 PM. Reason: Users private info including email removed |
| Sponsored Links | ||
|
|
|
||||
|
Yes, sed, grep and friends can do the trick. Here is sed example:
Code:
sed -i"_$(date +"%d_%m_%y_%H_%M_%S%P").bak" 's/ErrorLog/#ErrorLog/g' /path/to/input.conf To update all files in /usr/local/apache/conf/vhosts/ you need to write a for loop or something like that... Create a script called /root/scripts/updatevhost.sh Code:
#!/bin/bash
_INPUT=$1
_NOW=$(date +"%m-%d-%y")
_BAKUPDIR=/root/bakup/httpd/$NOW
usage(){
echo "Usage: $(basename $0) /path/to/vhost.httpd.conf"
exit 1
}
[ $# -eq 0 ] && usage
[ ! -d $_BAKUPDIR ] && mkdir -p $_BAKUPDIR
if [ -w $_INPUT ]
then
/bin/cp $_INPUT "$_BAKUPDIR/$(basename $_INPUT).$(date +"%d_%m_%y_%H_%M_%S%P").bak"
sed -i'.bak' 's/ErrorLog/#ErrorLog/g' $_INPUT
else
echo "Error: File $_INPUT is not writable"
exit 2
fi
Code:
find /usr/local/apache/conf/vhosts/ -iname "*.conf" -print0 | while read -d $'\0' file; do /root/scripts/updatevhost.sh "$file"; done
__________________
Vivek Gite Linux Evangelist |
![]() |
| Tags |
| find , finding file , sed , shell scripting , updating files with sed |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| To find and replace in text file | nashtech | Shell scripting | 5 | 01-31-2010 06:32 AM |
| How to Redirect OutPut of Shell Command to a text file | Hadi | Shell scripting | 1 | 04-29-2009 07:27 PM |
| Modify Text in a file | maxcell | Shell scripting | 3 | 10-25-2008 08:33 PM |
| rearranging columns in a text file | sureshbup | Shell scripting | 2 | 12-06-2006 09:43 AM |
| Replacing text in a file using awk | postyrus | Shell scripting | 4 | 05-02-2005 03:31 PM |