在shell脚本中创建删除功能

时间:2018-05-21 14:39:34

标签: shell

而不是删除它重复的记录并创建类似的记录,我该如何解决这个问题:

Delete()
{
            clear
            f=0
            echo "Enter Employee NO: \c"
            read no

            for j in `cat Employee.txt`
            do
                        eno=$(echo "$j" | cut -d "," -f 1)
                        enm=$(echo "$j" | cut -d "," -f 2)
                        eadd=$(echo "$j" | cut -d "," -f 3)
                        eage=$(echo "$j" | cut -d "," -f 4)
                        egen=$(echo "$j" | cut -d "," -f 5)
                        edes=$(echo "$j" | cut -d "," -f 6)
                        ebal=$(echo "$j" | cut -d "," -f 7)

                        if [ $no -eq $eno ]
                        then
                                    f=1                              
                                    line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,false")
                                    fnm=`cat Employee.txt`
                                    d=$(echo "$fnm" | sed s/$j/$line/g )
                                    echo $d > Employee.txt          
                                    echo "                 Delete Successfully                           "
                        fi
            done
            if [ f = 0 ]
            then
                          echo "               No Record Found              "
            fi
}

1 个答案:

答案 0 :(得分:0)

您可以使用awk单行

echo "Enter Employee NO: \c"
read no

awk -F ',' -v eno=$no '{if ($1 == eno ){$8 = "false" }OFS=",";print }' \
Attendance.txt >tmp_file  && mv tmp_file Attendance.txt