Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've a string separated with commas where i want to find my input in that line

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

The output is empty! How should i use grep to find my input in that string?

share|improve this question
up vote 11 down vote accepted

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.

share|improve this answer

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"
share|improve this answer

I would use $echo US | grep -i "US|CA|JP|CH"

Instead of eco give it a list or something, where it can actually can go and look for patterns, instead of looking for them in "US"

share|improve this answer
    
You forgot -E. – reinierpost 6 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.