Home > Software design >  How to get first and last names having 6 characters long using regular expression in linux
How to get first and last names having 6 characters long using regular expression in linux

Time:10-30

I want to find all the names having first and last names 6 characters long only. I have write this grep '^.{6},.{6}$' names.txt but it is not giving any output. The names are written in file names.txt. Sample -> firstname,lastname

CodePudding user response:

grep '^.\{6\},.\{6\}$' names.txt

CodePudding user response:

If you are using basic regular expressions, you have to escape the curly braces.

Using you pattern, you can add -E for extended regular expressions so you don't have to escape them.

grep -E '^.{6},.{6}$' names.txt

Note that a dot in the pattern can also match a space or a comma, so the pattern could also match:

,,,,,,,,,,,,, or ,

You could update the pattern to match 6 times any character except for spaces, tab or a comma using a negated character class [^,[:blank:]]

grep -E '^[^,[:blank:]]{6},[^,[:blank:]]{6}$' names.txt
  • Related