我正在尝试读取 csv 文件中的每一列,然后使用 csv 列中的数据搜索目录。读取完所有行后,我想创建一个新目录,将文件复制到该目录,最后压缩该目录。关于如何做的任何想法?
以下是 CSV 文件的示例:
add an example here...
我不知道,如果您想以不同的方式处理每一列,但如果没关系,这里有一个脚本的草稿。
separator=";" # select separator type in your csv file
csv_file="`pwd`/file.csv" # path to your csv file
search_dir="`pwd`/searchdir" # directory, where we search for files, that are in csv files
copy_to_dir="`pwd`/newdir" #directory where to save copied files
mkdir $copy_to_dir #making copy dir
for i in `cat ${csv_file} | tr $separator '\n'` ; do # reading all the entries in csv file
find $search_dir -name "$i" | xargs -i cp {} $copy_to_dir
# assuming, that all entries in csv are files to be found in a dir and copied
done
zip -j -r copied_files.zip $copy_to_dir # zip found contents
rmdir $copy_to_dir # if you need just the zip file
This is a draft of the script based on the informations you gave:
destdir="/path/to/destination/directory"
filelist=""
while IFS=, read col1 col2 col3 col4 ...
do
# check if the file should be included in filelist
if [ ... ]; then
filelist+="$col1/$col2/$col3.$col4 "
fi
if [ ! -z "$filelist" ]; then
mkdir -p $destdir
for goodfile in $filelist; do
cp $goodfile $destdir
done
fi
done < file.csv
To make things clearer I assumed the columns in the CSV file are just the parts of the path and name of the files you're interested in. Hope this helps. :)