I’m trying to write a script with a file as an argument that greps the text file to find any word that starts with a capital and has 8 letters following it. I’m bad with syntax so I’ll show you my code, I’m sure it’s an easy fix.
grep -o '[A-Z][^ ]*' $1
I’m not sure how to specify that:
a) it starts with a capital letter, and
b)that it’s a 9 letter word.
Cheers
EDIT:
As an edit I’d like to add my new code:
while read p
do
echo $p | grep -Eo '^[A-Z][[:alpha:]]{8}'
done < $1
I still can’t get it to work, any help on my new code?
'[A-Z][^ ]*'will match one character between A and Z, followed by zero or more non-space characters. So it would match any A-Z character on its own.Use
\bto indicate a word boundary, and a quantifier inside braces, for example:If you just did
grep '[A-Z][a-z]\{8\}'that would match (for example) “aaaaHellosailor”.I use
\{8\}, the braces need to be escaped unless you usegrep -E, also known asegrep, which uses Extended Regular Expressions. Vanillagrep, that you are using, uses Basic Regular Expressions. Also note that\bis not part of the standard, but commonly supported.If you use
^at the beginning and$at the end then it will not find “Wiltshire” in “A Wiltshire pig makes great sausages”, it will only find lines which just consist of a 9 character pronoun and nothing else.