PDA

View Full Version : Selecting files



achuth
February 20th, 2012, 02:56 PM
Sir,
I have 5 files in my folder ABCD ABCd ABcd aBcD and abcd
I would like to select (using ls command) those files which have exactly two lower case alphabets in their name.

I used the following command, but it failed

ls *[a-b]*[a-b]*

How can I select the files ABcd and aBcD ?

cortman
February 20th, 2012, 04:05 PM
I assume you just want to list them with ls? Use egrep-


ls | egrep '^[A-Z]*[a-z][A-Z]*[a-z][A-Z]*$'

This will match files with any number of upper case letters and only two lowercase. With Perl you could make a far more elegant regex, but this will work.

ofnuts
February 20th, 2012, 09:56 PM
Sir,
I have 5 files in my folder ABCD ABCd ABcd aBcD and abcd
I would like to select (using ls command) those files which have exactly two lower case alphabets in their name.

I used the following command, but it failed

ls *[a-b]*[a-b]*

How can I select the files ABcd and aBcD ?


ls *([[:upper:]])[[:lower:]]*([[:upper:]])[[:lower:]]*([[:upper:]])
# or
ls *([![:lower:]])[[:lower:]]*([![:lower:]])[[:lower:]]*([![:lower:]])
(yes, bash regexps are very verbose)

PS: things like [a-z]/[A-Z] won't usually work (unless you set LC_COLLATE to 'C')

cortman
February 20th, 2012, 10:57 PM
ls *([[:upper:]])[[:lower:]]*([[:upper:]])[[:lower:]]*([[:upper:]])
# or
ls *([![:lower:]])[[:lower:]]*([![:lower:]])[[:lower:]]*([![:lower:]])
(yes, bash regexps are very verbose)

PS: things like [a-z]/[A-Z] won't usually work (unless you set LC_COLLATE to 'C')

I must have missed the "ls" only part of the OP's question... egrep sure makes it a little cleaner. I didn't know what regex flavor ls supported.

achuth
February 21st, 2012, 05:53 AM
I would like to list only files with exactly two lower case alphabets in it.

ofnuts
February 21st, 2012, 09:04 AM
I would like to list only files with exactly two lower case alphabets in it.So what doesn't work in the code bits above (whether it's sed or ls?)

achuth
February 22nd, 2012, 05:16 AM
Yes ok. It is working fine.
Thanks.
But please explain the following command a bit more.

ls *([[:upper:]])[[:lower:]]*([[:upper:]])[[:lower:]]*([[:upper:]])

Vaphell
February 22nd, 2012, 06:27 AM
http://www.linuxjournal.com/content/bash-extended-globbing

*(pattern-list) Matches zero or more occurrences of the given patterns


*([[:upper:]]) [[:lower:]] *([[:upper:]]) [[:lower:]] *([[:upper:]])

ofnuts
February 22nd, 2012, 08:32 AM
Yes ok. It is working fine.
Thanks.
But please explain the following command a bit more.

ls *([[:upper:]])[[:lower:]]*([[:upper:]])[[:lower:]]*([[:upper:]])


>=0 uppercase
1 lowercase
>=0 uppercase
1 lowercase
>=0 uppercase

That makes anything alphabetic with exactly two lowercase characters.