0

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?

4

3 回答 3

2

'[A-Z][^ ]*'将匹配 A 和 Z 之间的一个字符,后跟零个或多个非空格字符。所以它会自己匹配任何 AZ 字符。

用于\b指示单词边界和大括号内的量词,例如:

grep '\b[A-Z][a-z]\{8\}\b'

如果您刚刚这样做grep '[A-Z][a-z]\{8\}',则将匹配(例如)“aaaaHellosailor”。

我使用\{8\},大括号需要转义,除非你使用grep -E,也称为egrep,它使用扩展正则表达式。您正在使用的Vanillagrep使用基本正则表达式。另请注意,这\b不是标准的一部分,但通常受支持。

如果你^在开头和$结尾都使用,那么它不会在“威尔特郡猪做伟大的香肠”中找到“威尔特郡”,它只会找到由 9 个字符的代词组成的行。

于 2012-10-22T18:19:35.247 回答
1

这对我有用:

$ echo "one-Abcdefgh.foo" | grep -o -E '[A-Z][[:alpha:]]{8}'
$ echo "one-Abcdefghi.foo" | grep -o -E '[A-Z][[:alpha:]]{8}'
Abcdefghi
$ 

请注意,这不处理扩展名或前缀。如果您想强制输入为 9 字母大写单词,我们需要更明确:

$ echo "one-Abcdefghij.foo" | grep -o -E '\b[A-Z][[:alpha:]]{8}\b'
$ echo "Abcdefghij" | grep -o -E '\b[A-Z][[:alpha:]]{8}\b'
$ echo "Abcdefghi" | grep -o -E '\b[A-Z][[:alpha:]]{8}\b'
Abcdefghi
$ 
于 2012-10-22T17:59:37.750 回答
0

I have a test file named 'testfile' with the following content:

Aabcdefgh
Babcdefgh
cabcdefgh
eabcd

Now you can use the following command to grep in this file:

grep -Eo '^[A-Z][[:alpha:]]{8}' testfile

The code above is equal to:

cat testfile | grep -Eo '^[A-Z][[:alpha:]]{8}'

This matches

Aabcdefgh
Babcdefgh
于 2012-10-22T18:22:10.333 回答