【问题标题】:Linux shell: how to Iterate multiple file lists and take action on each line in the file?Linux shell:如何迭代多个文件列表并对文件中的每一行采取行动?
【发布时间】:2016-10-03 07:27:19
【问题描述】:

我正在尝试迭代目录中具有 FILELIST 扩展名的所有文件,我设法做到了。但后来我想阅读这些文件的内容,包含其他文件的路径和文件名。这些文件,我想移到另一个位置。

FileA.FILELIST
/somepath/File1.csv
/somepath/File2.csv
FileB.FILELIST
/somepath/File3.csv
/somepath/File4.csv

到目前为止我所拥有的......

#!/bin/bash
# Iterate all file lists
for fl in /path/Inbox/*.FILELIST
do
  #Iterate the content of the current file list
  while read line; 
  do
    #Move it to the Archive directory...
  done < $fl
done

提前致谢!!

【问题讨论】:

  • 只需将#Move... 行替换为mv "$line" /archive/dir

标签: linux list shell file loops


【解决方案1】:

试试这个..

ls *.FILELIST|while read file    # Reading all files named ".FILELIST" - 1 by 1.
do
    echo "File is $file"    # Your current file in the list

    cat $file|while read line    # Now reading the lines of the file
    do
        echo "Line is $line"
    done
done

所提供输入的示例输出。

 >Wed Oct 05|01:54:14|gaurav@[STATION]:/root/ga/scripts/temp/tmp % ls -lrtha *.FILELIST
-rw-rw-r--. 1 gaurav gaurav 40 Oct  5 01:52 FileA.FILELIST
-rw-rw-r--. 1 gaurav gaurav 40 Oct  5 01:52 FileB.FILELIST
 >Wed Oct 05|01:54:18|gaurav@[STATION]:/root/ga/scripts/temp/tmp % cat *.FILELIST
/somepath/File1.csv
/somepath/File2.csv
/somepath/File1.csv
/somepath/File2.csv
 >Wed Oct 05|01:54:23|gaurav@[STATION]:/root/ga/scripts/temp/tmp % ./a.sh
File is FileA.FILELIST
Line is /somepath/File1.csv
Line is /somepath/File2.csv
File is FileB.FILELIST
Line is /somepath/File1.csv
Line is /somepath/File2.csv
 >Wed Oct 05|01:54:26|gaurav@[STATION]:/root/ga/scripts/temp/tmp %

【讨论】:

    【解决方案2】:

    您的脚本看起来不错,并且通过如下所示的一些调整,应该可以为您完成这项工作。我在read 中添加了条件来处理您正在读取的文件中的特殊字符。

    #/bin/bash
    
    for file in /path/Inbox/*.FILELIST
    do
        while IFS= read -r -d '' line;
        do
            echo "$line"
    
            # mv "$line" "$targetPath"
            # Do whatever else you want to do with the line here
    
        done < "$file"
    done
    

    【讨论】:

      猜你喜欢
      • 2014-02-17
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      相关资源
      最近更新 更多