Skip to main content

Word Boundaries

Because grep defaults to substring matching, searching for short words often results in overwhelming noise.

Imagine you want to find all processes running the command is.

ps aux | grep "is"

This will return everything: redis, history, systemd-resolved, disks, etc.

The Word Boundary Flag (-w)

To solve this, use the -w (--word-regexp) flag. This tells grep that the pattern must be preceded and followed by a non-word character.

"Word characters" in UNIX are defined as letters, digits, and underscores ([a-zA-Z0-9_]). "Non-word characters" are spaces, punctuation, hyphens, and the beginning or end of the line.

# Will match " is ", "is.", or "is-active", but NOT "redis"
grep -w "is" file.txt

Real World Example: Auditing Users

If you are auditing /etc/passwd to ensure the user root is configured correctly, a standard grep is dangerous.

grep "root" /etc/passwd

This might return: root:x:0:0:root:/root:/bin/bash (The actual root user) nm-openvpn:x:114:120:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin (Contains "chroot")

To isolate the actual root user definition, we can use word boundaries (or regex anchors).

grep -w "root" /etc/passwd

The Line Boundary Flag (-x)

Taking boundaries a step further, the -x (--line-regexp) flag forces the pattern to match the entire line, from the very first character to the very last.

If the file contains user=admin, searching for admin with -x will fail. It must be an exact 1:1 match of the line.

# Will ONLY match lines that contain nothing but "success"
grep -x "success" audit.log

Practical Use Case: Comparing Lists

-x is incredibly useful when combined with the -f flag (read patterns from a file) to find exact matches between two lists, ensuring that "apple" doesn't falsely match a line containing "applesauce".

# Find exact lines from list1.txt that also exist exactly in list2.txt
grep -x -f list1.txt list2.txt