2016-10-03 108 views
2

我想遍历FILELIST扩展名的目录中的所有文件,我设法做到这一点。但后来我想阅读这些文件的内容,其中包含其他文件的路径和文件名。这些文件,我想移动到另一个位置。Linux shell:如何迭代多个文件列表并对文件中的每一行执行操作?

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 

在此先感谢!

+1

只需用'mv“替换'#Move ...'行$ line”/ archive/dir' – anubhava

回答

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|[email protected][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|[email protected][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|[email protected][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|[email protected][STATION]:/root/ga/scripts/temp/tmp % 
1

你的脚本看起来很不错,并且像下面这样的一些调整,应该为你做这项工作。我在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 
相关问题