简体   繁体   中英

Grep Regular Expression in Cygwin

I am using Cygwin on Windows. I want to extract all the lines from a file which contain exactly 9 letters in the name.

To do this, I am using:

cat filename.txt | grep -P "[a-z]{9}"

however this is also returning words of different case and lengths greater than 9.

I have even set the environment variable, LC_ALL to C.

I am able to make this work though:

cat filename.txt | grep -P "^[az]*[az]$"

And this displays only words with lowercase characters.

Please note that I am running the commands in Cygwin and I have observed that there are certain differences between Cygwin and a Linux Distro. The commands do not work the same way.

Try

cat filename.txt | grep -P "^[a-z]{9}$"

^ = beginning of string

$ = end of string

Your regex returns all words containing lower-case alphabets which have a length which is a multiple of 9.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM