awk模式匹配 - 将awk值与输入进行比较

时间:2016-12-13 03:19:38

标签: bash awk

我需要一些帮助来编写一个脚本来比较运行awk和用户输入的值。 即: cat employee.txt 100 Thomas Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Sanjay Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500 500 Randy DBA Technology $6,000 EMPLOYERS=$(awk -F" " '{ print $2}' employee.txt) echo "Enter the name of an employer: \c" read employer If/for/while employer is part of EMPLOYERS, echo a message.

提前致谢

2 个答案:

答案 0 :(得分:2)

或只是

...
awk -v emp=$employer '$2==emp{print "found"}' employee.txt

-v标志用于定义awk变量,此处将shell值传递给脚本。

答案 1 :(得分:1)

我会更简单:

echo "Enter the name of an employer: \c"
read employer
if [[ $(awk -F" " '{ print $2}' employee.txt | grep $employer) = $employer ]]
then 
    echo Found
fi