hey everyone! I'm trying to come up with a pattern that matches all non-hidden files in a directory that do not start with cs. I've tried all sorts of things which are fairly obvious why they do not work. The problem I'm having is coming up with something that does work.
I've tried [!.c][!s]* which means doesn't start with a . or c and then a character that is not a s. The problem here is we cannot match cy which is valid.
!{.,cs}* which is non-sense as ! only means not when enclosed in a character class.
echo [!(. || (c && s))]* this doesn't work because we don't have logical operators in the pattern model we are using. This doesn't work anyways since it means first character is not a dot or is not c and s. The first character cannot be both c and s at the same time.
I thought to write a really long character class for the first and 2nd characters but this wouldn't allow us to say s is a valid 2nd character when the first character is not c.
So I'm completely stuck at how to do this. The only quantifiers I'm allowed to use are:
? match one character
* match 0 or more characters
{} match any alternative in the set
[] character set
[! ] match any character not in the set.
Thanks for any help!!
Globbing pattern troubles: match all hidden files not starting with cs
Started by chili5, Sep 21 2011 06:39 AM
4 replies to this topic
#1
Posted 21 September 2011 - 06:39 AM
|
|
|
#2
Posted 21 September 2011 - 08:05 AM
I don't think you can do that with just a pattern. You could do something like this:
Also, the *nix CLI doesn't use the [! ] metacharacter, and { } is used for a range. You are using a *nix, aren't you? Because if this is a question about C# or .NET, then it doesn't belong here.
ls -1 | grep -v "^cs"
Also, the *nix CLI doesn't use the [! ] metacharacter, and { } is used for a range. You are using a *nix, aren't you? Because if this is a question about C# or .NET, then it doesn't belong here.
Programming is a journey, not a destination.
#3
Posted 21 September 2011 - 09:00 AM
I am using ubuntu. It actually is possible. What I wasn`t aware of is with {}. If you are using grep then {} is used for range. If you are using echo {} actually means match one of the following patterns. ex: {a,b,c} matches a, b or c. What I was not aware of is you could match patternms with the {} wildcard.
So the soln is: {c[!s]*, [!c.]?*}.
So the soln is: {c[!s]*, [!c.]?*}.
#4
Posted 21 September 2011 - 05:00 PM
I am not familiar with that notation, or with using globs in echo for that matter. Globs are usually used for searching or for doing something with a list of files. You should also realize that there's a difference between a glob and a regular expression. A glob uses the following metacharacters:
Regular expressions are more complex and are only used by certain programs like grep and sed. They use a wide variety of metacharacters, depending on what program it is, but the base ones are:
Globs are (at least by my assessment) not powerful enough for what you're trying to do. You should use regular expressions.
Also, I don't understand how you can use a glob in echo. echo can't read a directory; it can only interpret characters and environment variables. If what you're trying to do is list files, or do something with files that don't start with cs, you should use ls and grep, or alternatively something like this:
? Any one character * Any string of characters [ ] Anything in the brackets [^ ] Anything not in the brackets
Regular expressions are more complex and are only used by certain programs like grep and sed. They use a wide variety of metacharacters, depending on what program it is, but the base ones are:
. Any one character
* Any number of the preceding
[ ] Anything in the brackets
[^ ] Anything not in the brackets
^ Beginning of a line or string
$ End of a line or string
{n} Exactly n occurrences
{m,n} Between m and n occurrences
\ Escape character
Globs are (at least by my assessment) not powerful enough for what you're trying to do. You should use regular expressions.
Also, I don't understand how you can use a glob in echo. echo can't read a directory; it can only interpret characters and environment variables. If what you're trying to do is list files, or do something with files that don't start with cs, you should use ls and grep, or alternatively something like this:
for file in *
do
filter=$(echo "$fil" | grep -v "^cs");
if [ ${#filter} -gt 0 ]
then
# do stuff with file here
fi
done
Programming is a journey, not a destination.
#5
Posted 27 September 2011 - 03:53 AM
In addition to the glob characters you showed it also supports this one:
which matches any one of the alternatives in the brace brackets. Also I'm using bash so the character for not is ! not ^ as with regular expressions. I'm more familar with regular expressions than the globs and noticed right away that they are a lot less possible.
Also if your folder has two files file1 and file2 and you type this:
the output is
file1 file2
not file*
The reason for this is the bash shell expands the glob before echo is called. So if I do echo Hello the output is Hello (but this because Hello did not get expanded to anything) so the shell just left it as Hello.
{}
which matches any one of the alternatives in the brace brackets. Also I'm using bash so the character for not is ! not ^ as with regular expressions. I'm more familar with regular expressions than the globs and noticed right away that they are a lot less possible.
Also if your folder has two files file1 and file2 and you type this:
echo file*
the output is
file1 file2
not file*
The reason for this is the bash shell expands the glob before echo is called. So if I do echo Hello the output is Hello (but this because Hello did not get expanded to anything) so the shell just left it as Hello.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









