Skip to main content

Grep

Basic

grep between two patterns

grep -oP '(?<=PATTERN1).*(?=PATTERN2)'

The regex pattern: '(?<=PATTERN1).*(?=PATTERN2)'

Example:
To grep the content in between user:[ and ] rid: from users.txt file.

user:[Administrator] rid:[0x1f4]  
user:[Guest] rid:[0x1f5]
user:[krbtgt] rid:[0x1f6]
user:[audit2020] rid:[0x44f]
cat users.txt | grep -oP '(?<=user:\[).*(?=\] rid:)'

grep between two patterns - include pattern

cat users.txt | grep -oP 'user:\[.*\] rid'

grep word of length

Return the words with length greater than 7 and less than 8

grep -o -w '\w\{7,8\}' user-list.txt

Reference

Awesome video by on grep by Robert