A general method to sort by an arbitrary function of the contents of the line is as follows:
- Get the key you want to sort by, and copy it to the beginning of the line
- Sort
- Delete the key from the beginning of the line
Here is a key you can use in this particular case: this sed
program will output the the line from the last identifier to the end.
% sed -e 's/^.*[^[:alnum:]_]\([[:alpha:]][[:alnum:]_]*\)/\1/' < decls
albumArtView; // 1
profileView; // 2
postFB; // 3
saveButton; // 4
To put these keys and the original lines side by side:
% paste <(sed -e 's/^.*[^[:alnum:]_]\([[:alpha:]][[:alnum:]_]*\)/\1/' < decls) decls
To sort them ...
| sort
and to leave just the second field (the original line)
| cut -f 2-
All together (sorting in reverse order, so there's something to show):
% paste <(sed -e 's/^.*[^[:alnum:]_]\([[:alpha:]][[:alnum:]_]*\)/\1/' < decls) decls \
| sort -r \
| cut -f 2-
→
@property (nonatomic, assign) UIButton *saveButton; // 4
@property (nonatomic, strong, readonly) UIImageView *profileView; // 2
@property (nonatomic, strong, readwrite) UIButton *postFB; // 3
@property (nonatomic, strong) id <AlbumArtDelegate, UITextFieldDelegate> *albumArtView; // 1
Best Answer
Find's
-name
option supports file globbing. It also supports a limited set of regex-like options like limited square-bracket expressions, but for actual regex matches, use-regex
.If you're looking for a match in the contents of a file, use
grep -r
as Craig suggested.If you want to match the filename, then use
find
with its-regex
option:Note the shift in regex, because
find
doesn't portably support bracketed atoms in its regex. If you happen to be on a Linux system, GNU find supports a-regextype
option that gives you more control:Note that if all you're looking for is case matching,
-iregex
or even-iname
may be sufficient. If you're usingbash
as your shell, Gilles' globstar solution should work too.