One-liners for using grep: Match lines where the first 2 characters match the last 2 characters: $ grep -e '^\(..\).*\1$' This would correctly match a line like: ealeigaklejgalegkjaegea