How can I display all files in current directory where the first character is numeric and last character is non alphanumeric. I already try with :
ls grep -E '^{[-0-9]
but this command line did not give me results as expected.
You don't need grep
at all - you can us the bash shell's built-in pattern matching (globbing) feature. For example (assuming "numeric" in this context means a decimal digit):
ls [0-9]*[^0-9A-Za-z]
or
ls [[:digit:]]*[^[:alnum:]]
See for example
Note that if you want to view the files' contents as per your title, you will need something like cat
(or perhaps head
) in place of ls
.
*
wildcard where regex would have .*
for example). I'm cautious about simplifying because the expansions are locale-dependent, as explained in the manual that I linked.
Dec 3, 2019 at 11:24
Try this:
mtodorov@efk:~$ ls | grep -E '^[0-9].*[^0-9A-Za-z]$'
0abc%
1abc%
mtodorov@efk:~$
Explanation: ^
marks the beginning and $
marks the end of a regular expression, .*
is a wildcard like *
in shell, ^
within brackets negates the selection and the single quotes must be closed.
a-Z
is a condensed writing for a-zA-Z
, where A-z
would take character between Z
and a
(6 non alphabetical character); you must try this expression, you would be surprised
Using regex matching with find:
$ find -maxdepth 1 -type f -regextype posix-extended \
-regex '.*/[[:digit:]].*[^[:alnum:]]$' -printf %f\\n
find options:
-maxdepth n
Descend at most n levels.-type f
Limit the type to regular files.-regextype type
RE syntax used by -regex.-regex pattern
RE pattern used to match the whole path.-printf format
will print formatted output..*
Matches any character zero or more times.[]
Matches a single character contained within the brackets[^]
Matches a single character that is not contained within the brackets.[:digit:]
POSIX standard defined class for digits.[:alnum:]
POSIX standard defined class for alnum.$
Matches end of line anchor.
ls
andgrep
, no close "'" bracket.. etc so I'd suggest ensuring you're asking us exactly what you tried, as it doesn't look like it'll work to me either.