How to extract only 7 characters using grep
16,314
Solution 1
Use extended grep:
grep -E '^[a-zA-Z0-9]{7}$' /usr/share/wordlists/rockyou.txt
or your own version like:
grep '^[a-zA-Z0-9]\{7\}$' /usr/share/wordlists/rockyou.txt
or even:
egrep '^.{7}$' /usr/share/wordlists/rockyou.txt
Solution 2
Any line that contains more than 7 characters also contains a substring of 7 characters (which will match your expression).
You can force only complete matches by anchoring the expression to the start and end of the line:
grep '^[a-zA-Z0-9]\{7\}$' /usr/share/wordlists/rockyou.txt
or specify whole-line matching using the -x
option
grep -x '[a-zA-Z0-9]\{7\}' /usr/share/wordlists/rockyou.txt
From man grep
:
-x, --line-regexp
Select only those matches that exactly match the whole line.
For a regular expression pattern, this is like parenthesizing
the pattern and then surrounding it with ^ and $.
Related videos on Youtube
Author by
user7897287
Updated on September 18, 2022Comments
-
user7897287 over 1 year
I am using a regular expression with
grep
. I want to extract exactly 7 character passwords, but I am getting 7 and more than 7 characters as a result.grep '[a-zA-Z0-9]\{7\}' /usr/share/wordlists/rockyou.txt grep '[a-zA-Z0-9]\{7,7\}' /usr/share/wordlists/rockyou.txt
-
Zanna about 3 yearsAs explained in steeldriver's answer, this will not work because all lines with more than seven characters will also match.