Cannot find my input in string using grep

Swap the arguments of the commands:

echo "US,CA,CH,JP" | grep US

In:

echo US | grep "US,CA,CH,JP"

you are looking for the string (pattern) US,CA,CH,JP in the input string US, which is not matching expectedly.


Unless you were expecting the commas to act like an "or" statement, in which case you just need to change the commas to pipes and use the -E option for Extended regular expressions:

echo US | grep -E "US|CA|CH|JP"

Or change the commas to newline characters:

echo US | grep "US
CA
CH
JP"

Tags:

Grep