Friday, May 27, 2022

[SOLVED] creating a shell script that does mutilple grep operation (AND operation)

Issue

I have a datafile as below.

datafile

Sterling|Man City|England
Kane|Tottenham|England
Ronaldo|Man Utd|Portugal
Ederson|Man City|Brazil

If I want to find a player that has a "Man City" and "England" trait I would write a command as such in the interpreter.

grep "Man City" datafile | grep "England"
output: Sterling|Man City|England

Like this, I want to make a shell script that receives multiple(more than 1) arguments that finds a line which has all the arguments in it. It would receive input as arg1 arg2 arg3... and return all the data lines that has the arguments. The program would run like this.

$ ./find ManCity England 
output: Sterling|Man City|England

But I have absolutely no idea how to implement the command that does multiple grep for each argument. Can you use a for loop and implement in a code that does something like this?

grep arg1 datafile | grep arg2 | grep arg3 ... 

Also, if there is a better way to find a data line that has all the arguments(AND of the arguments) can you please show me how?


Solution

You need this:

#!/bin/bash
myArray=( "$@" ) # store all parameters as an array
grep="grep ${myArray[0]} somefile" # use the first param as the first grep
unset myArray[0]; # then unset the first param
for arg in "${myArray[@]}"; do 
   grep="$grep | grep '$arg'" # cycle through the rest of the params to build the AND grep logic
done
eval "$grep" # and finally execute the built line


Answered By - Ron
Answer Checked By - Senaida (WPSolving Volunteer)