0

我在下面的代码中做错了什么?

我正在替换文本文件中的工资数据,但Telephone number正在更新字段(第 3 列)而不是salary0 的字段(第 5 列)。

在下面的示例中,Ruben 的计算工资为 500。

我想要的输出是:

Ruben,1223,97707001,Salaried,500

但相反,我得到了这个(用 535 替换 9770 和 7001 之间的零):

Ruben,1223,9775007001,Salaried,0

payroll_employee()
{

   echo
   echo "[Option: $input]"
   echo "Enter Payroll of an employee "
   echo
   echo -en "Enter employee name: "
   read Name

  #Retrieve current entry into individual fields                                                        
  line=`grep -i "$Name" $PAYROLL`
  Name=`echo $line | cut -d "," -f1`
  EmployeeID=`echo $line | cut -d "," -f2`
  EmployeeHP=`echo $line | cut -d "," -f3`
  EmployeeType=`echo $line | cut -d "," -f4` 
  Salary=`echo $line | cut -d "," -f5` 

  #Check if entry exist in records
   if [ `count_lines "^${Name},"` -eq 0 ]
   then
       echo "Error: This particular record does not exist!!"
   else
      echo "$Name is ${EmployeeType} employee."


   if [ "$EmployeeType" = "Salaried" ]
   then
    echo $EmployeeType  
    echo -en "Enter Weekly Salary:"
    read swages                     
    if [ -z $swages ]
        then
        swages=$Salary
        else     
        grep -vi "$Name" $PAYROLL > tmpfile      #Perform updating to salary field entry
        grep -x "$line" $PAYROLL | sed -e "s/$Salary/$swages/" >> tmpfile
        mv tmpfile $PAYROLL
        echo "$Name's weekly payroll has been updated to \$$swages!!"
        fi
    echo
}

示例代码:

update_employee()
{
  echo
  echo "[Option: $input]"
  echo "Updating employee record... "
  echo "Please enter the name of the employee to update: "
  echo -en "[1]Name: "
  read update_name

  #Retrieve current entry into individual fields                                                        
  line=`grep -i "$update_name" $PAYROLL`
  oldname=`echo $line | cut -d "," -f1`
  oldjob=`echo $line | cut -d "," -f2`
  olddept=`echo $line | cut -d "," -f3`
  oldsal=`echo $line | cut -d "," -f4` 

  #Check if entry to update exist in records
  if [ `count_lines "^${update_name},"` -eq 0 ]
  then
     echo "Error: This particular record does not exist!!"
  else
     while [ "$choice" != "6" ]
     do
        update_menu    #Display update menu for user input,allows update of individual field or all at once
        read update_choice
        case $update_choice in
             "1")  echo -en "Please enter employee's new name: " 
                   read new_name 
                   if [ -z $new_name ]
                   then
                      new_name=$oldname
                   elif [ `count_lines "^${new_name},"` -ne 0 ]  #Check if name already exist in records
                   then
                      echo "Error: Employee [$new_name] already exist in records!"
                   else  
                      grep -vi "$oldname" $PAYROLL > tmpfile     #Perform updating to name field entry
                      grep -x "$line" $PAYROLL | sed -e "s/$oldname/$new_name/" >> tmpfile
                      mv tmpfile $PAYROLL   
                      echo "Employee's name $oldname has been updated to [$new_name]!!"
                   fi
                   break
                   ;; }

我所做的只是再添加一列。

  Salary=`echo $line | cut -d "," -f5` 
4

2 回答 2

0

似乎您正在替换$Salary工资单行中每个出现的字符串,因此如果薪水是100并且员工 ID 号是2100,它将被替换。

而不是sed在最后使用,您可能会更好地使用生成输出printf并以这种方式构建字段。

就像是:

printf "%s,%s,%s,%s,%s\n" $Name $EmployeeID $EmployeeHP $EmployeeType $swages >> tmpfile

编辑:您应该按照评论中的说明修复=to 。==另外,为了说明我认为正在发生的事情:

line="ABC,5100,DEF,100"
Salary=100
echo $line | sed -e s/${Salary}/XXX/

ABC,5XXX,DEF,100

如果您通过将 a$放在搜索字符串的末尾来“锚定”查询,它将只匹配最后一个值。

echo $line | sed -e s/${Salary}$/XXX/ 

ABC,5100,DEF,XXX

在代码中添加一些echo语句来检查变量的状态......

于 2013-11-08T03:05:52.967 回答
0
payroll_employee()
{

   echo
   echo "[Option: $input]"
   echo "Enter Payroll of an employee "
   echo
   echo -en "Enter employee name: "
   read Name

  #Retrieve current entry into individual fields                                                        

  if [ $(grep -ciw "$Name" $PAYROLL) -eq 0 ]
  then
    echo "No matches found in $PAYROLL";
  else
   line=$(grep -iw "$Name" $PAYROLL);
  Name=$(echo $line | awk -F "," print $1);
  EmployeeID=$(echo $line | awk -F "," print $2);
  EmployeeHP=$(echo $line | awk -F "," print $3);
  EmployeeType=$(echo $line | awk -F "," print $4);
  Salary=$(echo $line | awk -F "," print $5);

  fi

   if [ "$EmployeeType" == "Salaried" ]
   then
    echo $EmployeeType  
    echo -en "Enter Weekly Salary:"
    read swages                     
    if [ -z $swages ]
        then
        swages=$Salary
        else  
        sed "/$Name/d" $PAYROLL
    echo "$Name $EmployeeID $EmployeeHP $EmployeeType $swages" >> $PAYROLL;
        echo "$Name's weekly payroll has been updated to \$$swages!!"
        fi
    echo
}

我更喜欢使用 AWK 进行分隔

您也可以使用删除行

sed "/$Name/d" $PAYROLL

为什么使用 grep -vi 将其存储在其他文件中并重命名并替换为原始文件(浪费内存和资源)

也应该用使用 $(..) 的新方法替换 "`" 反引号

还要确保在匹配字符串时使用 == 符号

同样在 grep 中使用 -w 也只是为了确保您选择正确的完整字符串 Ruben baruben rubenner 如果未给出 -w,所有将在 grep 中搜索

于 2013-11-08T08:09:00.910 回答