Bhargav Katkam Bhargav Katkam - 5 months ago 11
Linux Question

grep non matching port@server

I want a regexp for below input

echo "1280@lic1 1728@lic1 1280@lic2.local 1924@lic1"| grep -Po 'regexp'


I want output as words not containing 1280 as below

1728@lic1 1924@lic1

Answer

You can use this lookahead and lookbehind regex:

echo "1280@lic1 1728@lic1 1280@lic2.local 1924@lic1"| grep -Po '(?<=\s|^)(?!\S*1280)\S+'

1728@lic1
1924@lic1
  • (?<=\s|^) asserts there a whitespace or line start before our match
  • (?!\S*1280) asserts that there is no 1280 in the word

Pipe the result to tr '\n' ' ' if you want to remove newlines from this output.

Comments