Regex matching first word and any word

grepregex

I have a log file with a list of many entries. With grep regex I want to match the first word and another word which can be anywhere in the string.

For example, if I specified "user1" the search would search:

grep -E '^(IP_CONNECT|IP_DISCONNECT) user1' file.txt

However this won't match unless user1 is at the beginning of the string whereas I want to match it if it appears anywhere in the string. How is this done?

Best Answer

I think you mean you want to match lines that begin with either "IP_CONNECT" or "IP_DISCONNECT", followed by one or more of [most] any characters, followed by "user1".

grep -E '^(IP_CONNECT|IP_DISCONNECT).+user1' file.txt

You also might find tools like regexpal helpful in exploring this.