ubuntu linux shell脚本更改文本文件中一行的列值

ubuntu linux shell脚本更改文本文件中一行的列值

所有这些都是在以下情况下编写的:

情况1:

 echo "${name}:${identity}:${hp}:$now:"0"" >> info.txt      # write to info.txt

所以我会有这样的东西:Tom:8:987654:commission:0 # i assign it to 0 first

案例2:

echo -n "Enter employee's name:"
read name

S=`grep $name info.txt`;    

    if test -z $S 
then
echo "No details with employee name  $name"
else
echo "$S" | cut -d: -f4 // display 4th column.
echo 
echo -n "Enter hourly wage: "; read wage;
echo -n "Enter hours worked this week: "; read hours;

pay=$hours-40*$wage;          

echo -n "To update next payroll : press Y else N to go main menu: "     

read option
case "$option" in 
[Yy]*)  echo "Payroll have been updated."
    echo grep $name | sed -i s/\:*$/$pay/n info.txt //error         
    ;;
[Nn]*) 
    echo "Press Enter to view the Main"     
esac
fi

我的文本文件如下所示:

Tom:8:987654:commission:0
Tommy:7:12379813:hourly:0
Fan:5:2132131:salaried:0

我正在尝试将最后一列值替换为 $pay (并非所有列,只有用户的特定名称)可以说 $pay 是 10,$name 是 Tommy

这是预期的文本文件:

   Tom:8:987654:commission:0
    Tommy:7:12379813:hourly:10
    Fan:5:2132131:salaried:0

但我收到了这个错误:sed: -e expression #1, char 15: unknown option tos'`

我正在 ubuntu 终端上做。

答案1

替换整行:

sed -e "/$name/s/[0-9]*$/$pay/" -i info.txt

相关内容