【发布时间】:2019-06-16 20:48:28
【问题描述】:
我很难遍历多个文件,这些文件根据多个标准(由正则表达式定义)选择一个。例如,从下面列出的文件中,我只想选择具有 txt 扩展名的文件,但包含字符串“cat”的文件除外:
mousecatdog.txt
dogcowfox.gif
dogmousecat.txt
dogmousepig.txt
所以我只想从此列表中获取dogmousepig.txt。
我尝试使用负前瞻,但出现错误:
syntax error near unexpected token `('
当然看了论坛上的其他帖子,也尝试过加括号()或者启用shopt -s extglob来解决问题,但无济于事。
我使用的是 mac,脚本是 bash。
#!/bin/bash
for FILE in ^((?!cat).)*.txt
do
printf "\$FILE\n"
done
【问题讨论】:
-
尝试添加
if:for FILE in *.txt; do if [[ "$FILE" != *cat* ]]; then echo "$FILE" fi; done