#!/bin/bash # # Syntax: mass_qr [-h] expr1 expr2 file [...] # # Purpose: Replaces expression expr1 by expr2 in files. # To get a \/ in one of the strings, use \\\/. # The -h flag shows the help info for this command. # Created: May 11, 2000 by Bart De Schutter # Last revised: Jan 22, 2002 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: files if [ "$1" == "-h" ]; then sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi if [ $# -lt 3 ]; then echo >&2 echo >&2 "ERROR: ${0##*/} requires at least 3 input arguments." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi expr1="$1" expr2="$2" shift 2 echo >&2 echo >&2 "WARNING: This command will replace string $expr1 with" echo >&2 " $expr2 within the given files." echo >&2 " The original files are kept as backup (.bak) files." echo >&2 read -p "Continue? (y/n) [n] " answer echo >&2 if [ "$answer" != "y" -a "$answer" != "Y" ]; then exit 1 fi for fname in "$@" do bak_file="${fname}.bak" if [ -f "$fname" ]; then cp "$fname" "$bak_file" sed -e "s/${expr1}/${expr2}/g" \ "$bak_file" > "$fname" else echo >&2 echo >&2 "WARNING: file $fname not found." echo >&2 fi done