Regular Expression to show files with 15 characters
QUESTION 1 (13 mark开发者_运维技巧s)
On gaul, change your working directory to /bin/ (a)What will you get when executing pwd command? Explain why did you get that output? (b) Use a Unix command to display the file names (do not display any of the directory contents, if any) in the current working directory whose names: I. (2 marks) are of length exactly 15 charactersThis is the only one I can not get done on my assignment, i wrote two regular expressions but i can not tell which one is correct
ls /bin/ | grep -c '([0-9])([a-z])*{1,15}'
8ls /bin/ | grep -c '[0-9][a-z]*{1,15}'
45it has to only be characters A-Z, 0-9 and _
You're making it more complicated than it needs to be. It doesn't say to use regex, nor does it restrict it to those characters (actual filenames are not limited to those).
Look at this documentation.
Hint: You want to match 15 single characters using a string of one or more of the characters given (?, *, or [).
I think the following ls command is enough for your problem:
ls -d ???????????????
from ls man page:
-d, --directory
list directory entries instead of contents, and do not dereference symbolic links
and 15 "?" means, the name of dir/file should be exactly length of 15.
Awk is a handy tool for this job:
ls /bin | grep -ve "[^A-Za-z_]" | awk '{if (length == 15) print $0}'
精彩评论