function change {
echo "Please enter the LAST name of the person: "
read changelast
echo "Please enter the FIRST name of the person: "
read changefirst
echo "Please enter the NEW phone number as XXX-XXX-XXXX: "
read changephone
while [[ "$changephone" != [0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-
9] ]]
do
echo "Enter ONLY enter the phone number as XXX-XXX-XXX"
read changephone
done
}
我如何使用sed / egrep命令按顺序检查文件的名字和姓,然后只更改phonenumber?
答案 0 :(得分:0)
从shell变量设置awk变量,然后将它们与awk脚本中的列进行比较。
awk -v first="$changefirst" -v last="$changelast" -v phone="$changephone" \
'$1 == first && $2 == last { $3 = phone }
{ print }' asg7phonebook > asg7phonebook.new
if cmp -s asg7phonebook.new asgphonebook
then echo "SORRY PERSON NOT FOUND...\n"
rm asg7phonebook.new
else mv asg7phonebook.new asg7phonebook
fi
答案 1 :(得分:-1)
将变量中的旧电话号码设为
OLD_NO=`cat phonebook | grep $changelast | grep $changefirst | awk '{print $3}'`
then replace old with new no. as
sed -i 's/$OLD_NO/$changephone/' phonebook
注意:这将替换所有匹配号码,但由于您正在处理电话号码,因此每个人都有唯一的电话号码。