How about sed, awk, head and tail? This is not the prettiest, but seems to
work:
# cat foo
#first first
#second
#third
#first first
#second
#third

# ./replaceFirst first FIRST foo
#FIRST first
#second
#third
#first first
#second
#third

# ./replaceFirst fourth FIRST foo
fourth not found

# cat replaceFirst
#!/bin/bash
SEARCH=$1
TARGET=$2
FILE=$3

found=`grep -n "^#$SEARCH" $FILE`
if [ $? != 0 ]; then
  echo "$SEARCH not found"
  exit
fi
headLines=`echo $found | head -1 | awk -F: '{ print $1 }'`
lines=`wc -l $FILE | awk '{ print $1 }'`
let tailLines=$lines-$headLines
head -$headLines $FILE | sed -e "s/^#$SEARCH/#$TARGET/g"
tail -$tailLines $FILE

"Mike MacIsaac" <[EMAIL PROTECTED]>   (845) 433-7061

----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390

Reply via email to