grep unknown number of params

Hey i got trivial question but i cant figure it out. I want to grep a file for multiple unknown parameters. The user will enter these parameters at the command line. For example...

./program red apple filename

This would grep for the phrase red apple. But i cant just do $1 $2 because the user could have entered red gala apple or just apple. I tryed using "$@" but it still doesnt work. Any suggestions? Thanks.

Try this...

#!/bin/bash
search=$( echo $* | tr ' ' '|' )
egrep "($search)" input_file

--ahamed

didnt work.

did you try the same code?
Please paste the output run with -x option.

#!/bin/bash
set -x
search=$( echo $* | tr ' ' '|' )
egrep "($search)" input_file

--ahamed

ya i used exactly what you have. The output is too long for the parameters im giving it. The parameters i am searching should only output one line but its giving me every line in the file.

It will search the entire file and give the output. What is your requirement exactly by the way?

Can you paste the input file contents, the arguments you are giving and the expected output?

--ahamed

the file is too big to paste here but here is an example....
some info from the file:

78 16300 Charlotte Amalie town VI 12331 4741 0000003024 0000000002 +18344032 -064933536
78 18100 Charlotte Amalie East CDP VI 2836 951 0000000467 0000000000 +18337018 -064912684
78 19000 Charlotte Amalie West CDP VI 5422 1956 0000004574 0000001576 +18339165 -064960538

what i want to do is type in the command line ./program Charlotte Amalie East CDP

the program would then output the information from that line but not the line before or after. So it has to match the parameters exactly. Thats why i thought $@ would work because its a list of the parameters given. Also note that the example given is 4 parameters long. However it can be 3 parameters long for CHarlotte Amalie town. So the number of parameters differs.

Well, I think this is all you need!

#!/bin/bash
grep "$*" input_file

Right?

Initially I thought you wanted to have each argument as the search element.

--ahamed

1 Like

ya i tried that before but without the quotes. Thanks that seems to work now thank you a bunch!