我想使用AND grep操作,但我不知道如何操作

时间:2017-03-26 01:52:20

标签: regex bash shell unix grep

文件夹中有一个名为pro1的程序和一个名为file1的数据文件。

在数据文件中,

Alex|New york karlos|011-1234-5678
Karl|Chicago koroq|012-3456-7890
Richard|New york ntown|023-4567-8990

我想使用grep AND。

我写下这段代码,但它无法正常工作

for arg in $@; do
if[ count -eq 1]
then
  egrep -i $arg file1 | $temp
else
  egrep -i $arg $temp | $temp
fi
done

echo $ temp

当我输入“./pro1 Alex New york”时 我想打印Alex |纽约卡尔罗斯| 011-1234-5678

我该怎么做?

1 个答案:

答案 0 :(得分:1)

档案'mgrep':

#!/bin/bash

TEMP=/tmp/mgrep-$$
touch $TEMP

COUNTER=0
for arg in $@; do
   let COUNTER=COUNTER+1
   if [ $COUNTER -eq 1 ]; then
      grep -i "$arg" > $TEMP
   else
      TEMP2=/tmp/mgrep-$$-$COUNTER
      grep -i "$arg" $TEMP > $TEMP2
      rm $TEMP
      TEMP=$TEMP2
   fi
done
cat $TEMP
rm $TEMP

像这样运行:

cat file1 | mgrep one two three
相关问题