仅列出目录中与单词 BOZO 匹配且以“123”结尾的某些文件。或“456”
我正在尝试找出如何获取名为 BOZO 但仅以 123 或 456 结尾的文件的文件名列表。
文件为:
BOZO12389,
BOZO和3
BOZO和456
BOZO和5
BOZOhello123
所以该命令应该只显示“BOZOhello123”和“BOZOand456”
我无法弄清楚。我已经尝试过我能想到的所有形式的 LS 和 GREP。有趣的是,我们在课堂上尝试了大约10分钟,但没有人能做得到(包括老师)。
I'm trying to figure out how to get a list of file names for a file named BOZO but ending with ONLY 123 OR 456.
Files are:
BOZO12389,
BOZOand3
BOZOand456
BOZOand5
BOZOhello123
So the command should only display 'BOZOhello123' and 'BOZOand456'
I can't figure it out. I've tried all forms of LS and GREP that I can think of. The funny thing is, we tried to do it in class for about 10mins and no one could get it (including the instructor).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我做了以下操作并且成功了
I did the following and it worked
使用 shell 的 glob:
Using shell's globs:
使用正则表达式可以帮助您。命令
egrep
应该会有所帮助,因为它允许您使用正则表达式。您正在搜索 BOZO456 和 BOZO123 类型的文件
。句点
.
是通配符,允许您替换
。*
会让您重复 0 次或多次。通过放置大约 123 和 456 个圆括号,您将模拟一个 OR。因此,您希望任何字符重复 0 次或多次,后跟 123 或 456。
示例:
egrep "BOZO.*(456|123)" data
感谢 Nathan Fellman 的帮助并进行编辑。
Use regular expressions to help you. The command
egrep
should help, because it will allow you to use regular expressions.You're searching for files of the kind BOZO456 and BOZO123
A period
.
is a wild card, allowing you to substitute for<anything>
. The*
will let you repeat it 0 or more times. By placing around 123 and 456 round brackets, you will simulate an OR.Thus, you want any character repeated 0 or more times, followed by 123 or 456.
Example:
egrep "BOZO.*(456|123)" data
Thank you to Nathan Fellman for the help and edits.
您还可以使用find命令:
You could also use find command :
$ ll grep *BOZO*
也应该可以工作!$ ll grep *BOZO*
should work too!这应该没那么难。最简单的方法是
ls
目录,然后 grep 只查找您想要的内容:This shouldn't be that hard. The most naive way is to
ls
the directory and then grep for only what you want: