简体   繁体   中英

I want to use AND grep operation, but i don't know how

There are one program named pro1 and one data file named file1 in the folder.

In data file,

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

And I'd like to use grep AND.

I write down this code, but it didn't work

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

echo $temp

When I enter "./pro1 Alex New york" I want to print Alex|New york karlos|011-1234-5678

How can i do it??

File '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

Run it like this:

cat file1 | mgrep one two three

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM