Quick and Easy Regular Expression Command/Script to Run on Files in the Bash Shell


I often find it necessary to run regular expressions on, not just one file; but instead a range of files. There are perhaps dozens of ways this can be done, with varying levels of understanding necessary to do this.

The simplest way I have encountered utilizes the following syntax:

perl -pi -e "s///g" 

Here is an example where I replace the IP address in a range of report templates with a different IP address:

perl -pi -e "s/mysql:\/\/192.168.2.110/mysql:\/\/192.168.2.111/g" $reportTemplateLocation/*.rpt*

Basically, I am looking for a line which contains mysql://192.168.2.110, which I want to replace with mysql://192.168.2.111.

Here is an example of a bash script I call changeReportTemplateDatabase.sh, which I wrap around that command, to accomplish that same task with more elegance:

#!/bin/bash
#
# @(#)$Id$
#
# Point the report templates to a different database IP address.
reportTemplateLocation="/home/apphome/jboss-4.0.2/server/default/appResources/reportTemplates";

error()
{
    echo "$arg0: $*" 1>&2
    exit 1
}
usage()
{
        echo "Usage $0 -o  -n ";
}

vflag=0
oldip=
newip=
while getopts hvVo:n: flag
do
    case "$flag" in
    (h) help; exit 0;;
    (V) echo "$arg0: version 0.1 8/28/2010"; exit 0;;
    (v) vflag=1;;
    (o) oldip="$OPTARG";;
    (n) newip="$OPTARG";;
    (*) usage;;
    esac
done
shift $(expr $OPTIND - 1)

if [ "$oldip" = "" ]; then
        usage;
        exit 1;
fi
if [ "$newip" = "" ]; then
        usage;
        exit 1;
fi

echo "$0: Changing report templates to use the database at $newip from $oldip";
perl -pi -e "s/mysql:\/\/$oldip/mysql:\/\/$newip/g" $reportTemplateLocation/*.rpt*

Usage of the script is as simple as the command below. It will change every database reference on report templates in the directory referenced by the variable reportTemplateLocation to the new value.

./changeReportTemplateDatabase.sh  -o 192.168.2.110 -n 192.168.2.111

A further improvement, which may be useful to some, would be to make the directory a flag which can be edited at the command line.


One response to “Quick and Easy Regular Expression Command/Script to Run on Files in the Bash Shell”

Leave a Reply

Your email address will not be published. Required fields are marked *