开发者

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 characters

This 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}'

8

ls /bin/ | grep -c '[0-9][a-z]*{1,15}'

45

it 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}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜