如何逐行读取多个文件

PostMaloneM3m3r

假设我的文件数量不足

/mnt/traitement,我想逐行读取此文件夹中的每个文件,我该怎么做,我知道要逐行读取文件,您需要执行此代码

while read line
do
   #stuff
done < file

但我不知道如何定位多个文件。

塞尔吉·科洛迪亚兹尼

将while循环包装为for循环应该足够了:

for i in file1 file2 file3; do
    while IFS= read -r line
    do
         #stuff
    done < "$i"
 done

如果您只关心文本本身,则可以使用cat file1 file2 file3 | while IFS= read -r line,但要注意非换行终止的文件,因为cat该文件的最后一项会与新文件的第一行接合在一起。

另外,由于您提到了特定目录,因此可以将glob与for循环一起使用for i in /mnt/traitement/*像这样

for i in  /mnt/traitement/*   ; do
    if ! [ -f "$i" ]; then
        continue
    fi
    while IFS= read -r line
    do
         #stuff
    done < "$i"
 done

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章