Edit and replace the multiple values in a file in one iteration

Hi All,

I am preserving OLD and NEW values and want to replace the values in one go instead of using multiple sed and mv commands. Please help.

echo "\nEnter the new qStart time '${CODE}' - (Hit Enter for No Change): \c"
read NEW
echo "\nEnter the new qStop time '${CODE}' - (Hit Enter for No Change): \c"
read NEW1
echo "\nEnter the new tStart time '${CODE}' - (Hit Enter for No Change): \c"
read NEW2
echo "\nEnter the new tStop time  '${CODE}' - (Hit Enter for No Change): \c"
read NEW3

After reading the values, doing additional checks and replacing starts at below code. Please suggest how I can do it one go instead of opening the file muultiple times and moving it across.

sed s/'\(.*.exch."${CODE}".qStart.*\)"${CURRENT}"\(.*read\)/\1"${NEW}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

sed s/'\(.*.exch."${CODE}".qStop.*\)"${CURRENT1}"\(.*read\)/\1"${NEW1}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

sed s/'\(.*Delay."${CODE}".tStart.*\)"${CURRENT2}"\(.*read\)/\1"${NEW2}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

sed s/'\(.*Delay."${CODE}".tStop.*\)"${CURRENT3}"\(.*read\)/\1"${NEW3}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

You can cram all the sed commands into one script:

sed 's/.../.../g; s/.../.../g; s/.../.../g' file

Are those back references really necessary, e.g. to discriminate possible duplicates? If not, try

sed "s/${CURRENT}/${NEW}/g; s/${CURRENT1}/${NEW1}/g; s/${CURRENT2}/${NEW2}/g; s/${CURRENT3}/${NEW3}/g;' $LOCK_FILE
2 Likes

Thank you RudiC
Actually, I was trying to read the line in the file and replacing the 2nd column with the new value.