如何使用while循环仅读取特定模式的文件名 [英] How to use while loop only to read filenames of a specific pattern

查看:75
本文介绍了如何使用while循环仅读取特定模式的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在特定目录中有以下文件.

Suppose I have the following files in a particular directory.

ABC_TEXT_FILE_365.csv
ABC_TEXT_FILE_365_07h32.csv
BCD_TEXT_FILE_432.csv
BCD_TEXT_FILE_432_08h28.csv
FGB_TEXT_FILE_567.csv
FGB_TEXT_FILE_567_09h45.csv

现在,我只想读取其中具有 h * .csv模式的文件,然后执行更多操作.

Now I want to read only the files which has h*.csv pattern in them and then perform more operations.

但是我知道while read filename
将读取所有文件名仪式.有任何解决方法吗?请帮助

But I know while read filename
will read all the filenames rite.? Is there any workaround for this. Please help

推荐答案

我猜你的意思是所有与*h*.csv匹配的文件,在这种情况下,您可能希望使用这样的for循环:

I'm guessing you mean all files that match *h*.csv, in which case you might want to use a for loop like this:

for i in *h*.csv; do 
    while read line; do 
        # do some stuff, e.g. echo "$line"
    done < "$i"
done

如果可能有任何与模式匹配的目录,则可能还需要添加测试以确保循环中的每个项目都是一个文件.那应该是:

In case it is a possibility that you have any directories that match the pattern, you might want to also add a test to ensure that each item in the loop is a file. That would be:

for i in *h*.csv; do 
    if [ -f "$i" ]; then 
        while read line; do 
            echo "$line" 
        done < "$i"
    fi
done

这篇关于如何使用while循环仅读取特定模式的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆