nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Shell Script To change strings / text in a text file

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 ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-07-2009, 07:34 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 597
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Shell Script To change strings / text in a text file

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>
The goal that I have is I want to place a # in front of ErrorLog to disable logging on all 2300 vhosts. I am sure this can be done with SED or PERL. Either way would be fine. Also I have to do bulk edits on these vhosts alot. I would like to be able to modify this script as needed. For redirects and aliases etc...

Thanks for your help

Jaysunn

Last edited by nixcraft; 05-08-2009 at 06:02 PM. Reason: Users private info including email removed
Reply With Quote
  #2 (permalink)  
Old 05-08-2009, 05:58 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,697
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

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
It will comment out errorlog line and also save file as backup /path/to/input.conf_date.bak.

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
Now run it as follows
Code:
find /usr/local/apache/conf/vhosts/ -iname "*.conf" -print0 | while read -d $'\0' file; do /root/scripts/updatevhost.sh "$file"; done
PS: Try this in testing dummy environment first and once satisfied move to production. Also, make sure you have backup all files before you modify anything. I won't be responsible for data loss good luck.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply

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

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

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


All times are GMT +5.5. The time now is 02:03 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38