0

以下のコードで何が間違っていますか?

テキスト ファイル内の給与データを置き換えていますが、フィールド ( 5 列目) が 0Telephone numberではなく、フィールド (3 列目) が更新されています。salary

以下の例では、Ruben の計算された給与は 500 です。

私の望ましい出力は次のとおりです。

Ruben,1223,97707001,Salaried,500

しかし、代わりに、これを取得します (9770 と 7001 の間のゼロを 535 に置き換えます):

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
                   ;; }

私が変更したのは、列をもう 1 つ追加したことだけです。

  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

EDIT :コメントで指摘されているように=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 に答える