Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that.

Currently I tried this:

match = grep "Hello" file | grep -v "Hi" file

But that's not working properly

Hi try command substitution ( $( ... ) ):

match=$(grep "Hello" file | grep -v "Hi")
1 Like

Hello pavan,

If you are ok with awk then you could try following too once.

match=$(awk '/Hello/ && !/Hi/'  Input_file)

Thanks,
R. Singh