简体   繁体   English

在 bash 中使用正则表达式列出文件 | 忽略具有某些字符的文件

[英]List files using regex in bash | ignore file having certain characters

I am new to regex, I want to list file matching with given regex.我是正则表达式的新手,我想列出与给定正则表达式匹配的文件。 I have some samples files as shown in below picture我有一些示例文件,如下图所示

在此处输入图片说明

I want regex which should return below mentioned files:我想要正则表达式,它应该返回下面提到的文件:

test1.csv test2.csv test3.csv test9.csv.tt test1.csv test2.csv test3.csv test9.csv.tt

I want to use the find command for this and i also tried with below mentioned find command but could not achive my expected results我想为此使用 find 命令,我也尝试使用下面提到的 find 命令,但无法达到我的预期结果

find . -maxdepth 1 -type f -name "**test[0-9]*[^-a-zA-Z_]***.csv*" -exec ls -l {} +

I want to use same commnad to achieve my expected output only i want correct regex.我想使用相同的 commnad 来实现我的预期输出,只是我想要正确的正则表达式。

Anyone have any idea please do help!任何人有任何想法请帮忙!

Using find , you can try:使用find ,您可以尝试:

find . -type f -regex ./test[0-9]\\*.csv.\\* -exec ls -l {} +

Or, using sed , you can try或者,使用sed ,您可以尝试

$ ls -l | sed '/^d/d;/ test[0-9]*\\.csv.*/!d'

(not sure if digits are mandatory -- if they're, then replace * by + ) (不确定数字是否是强制性的——如果是,则将*替换为+

There are 2 commands in this sed .sed有 2 个命令。

  1. /^d/d deletes any row that starts with a d (exclude directories) /^d/d删除任何以d开头的行(排除目录)
  2. / test[0-9]*\\.csv.*/!d removes any row that doesn't match the regex you want to filter by. / test[0-9]*\\.csv.*/!d删除与您要过滤的正则表达式不匹配的任何行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM