0

So I want to pass 2 strings and filename from the command line and search for both the strings in the file. Sample input : script.sh file.txt AB 78
Sample output : 001,AB,cse,78

(where the file has this line among others that do not match)

My try:

`#/usr/bin/env bash`  
`grep '$2.*$3\|$3.*$2' $1`

However this doesn't work. Can anyone help?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
  • Possible duplicate of [grep multiple patterns and print results with the match pattern](http://unix.stackexchange.com/questions/319511/grep-multiple-patterns-and-print-results-with-the-match-pattern) – phk Jan 15 '17 at 15:36

1 Answers1

1

You need to change you single quote into double quotes to allow the $2 and $3 to be expanded into your search string:

#/usr/bin/env bash
grep "$2.*$3\|$3.*$2" $1

A better explanation is here:

https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash#6697781

Stephen Rauch
  • 4,209
  • 14
  • 22
  • 32