0

我想搜索文本文件的字段 5,无论是“工资”、“佣金”还是“每小时”,搜索后它都会存储类型中找到的数据。然后将分类 Person 所在的类型,并根据类型运行代码。我被卡住了,因为我不知道如何使用搜索功能并将其存储在临时字段中。

代码:

        payroll()
        {
          line=`grep -i "^${update_empID}," $data`
          empID=`echo $line | cut -d "," -f1`
          name=`echo $line | cut -d "," -f2`
          job=`echo $line | cut -d "," -f3`
          phone=`echo $line | cut -d "," -f4` 
          type=`echo $line | cut -d "," -f5`

           clear
           echo -e "Enter the pay"
           echo -en "Enter ID: "
           read empid_search

           #Check if particular entry to search for existed to perform deletion
           if [ `count_lines "^${empid_search},"` -eq 0 ]
           then
               echo "Error: This particular record does not exist!!"
           else
               echo "Please verify update of this employee's record: " #Prompt for confirmation of employee details
            echo
               echo "Employee's Details: "
               locate_lines "^${empid_search},"   #Find location of the entry     
               awk -F ',' '$5 == "Salaried Employee"' $PAYROLL > types

           if [$type="Salaried"]
           then
            echo "$name is a Salaried"
            echo "Enter Salary :"
            read salary

          if [$type="Hourly"]
           then
            echo "$name is a Hourly"
            echo "Enter employee's Hourly Wage :"
                        read hourly_wage
                        echo "Enter hours worked this week :"
                        read hours_worked



             echo "${empID},${name},${job},${phone},${Type},${salary}" >> tmpfile ; mv tmpfile $data
               echo " particulars has been updated!!"
               fi      
            else
            echo "f"     
           fi

        }

count_lines()
{
   grep -i "$@" $PAYROLL | wc -l            
}
#Function to locate lines that match $1
locate_lines()
{
  result=-1
  if [ ! -z "$1" ]
  then
     grep -i "$@" $PAYROLL    
     result=$?  #Returns 0 if previous command is successful
  fi
  return $result   
}

文本文件

3,Frak,IT,9765753,Salaried
1,May,CEO,9789292,Salaried
5,Samy,Sales user,92221312,Commission
2,Orange,cleaner,935233233,Hourly
4

1 回答 1

0

i want to search field 5 of the text file, whether it is "Salaried", "Commission" or "Hourly"

你应该像这样使用 awk:

awk -F ',' '$5 ~ (Salaried|Commission|Hourly) {print}' file

顺便说一句,你的 BASH 脚本有很多语法错误,如果你切换到 awk,编写脚本会容易得多。

awk 教程

于 2013-11-05T15:05:52.587 回答