I have a .csv file where column 8 (named filepath) contains the file location of a file I would like to copy to my home folder in Linux. Now I would like to loop over this column and use the cp function to copy the file to a folder in my home. I tried to do the following:
#!/bin/bash
while read filepath
do
for filepath in $filepath
cp $filepath /home/files
done
done < files.csv
This however does not seem to work. When I replaced the 'cp'in the for loop with an echo statement, it did not print out the file location, but the whole line of the file.
Thank you for your help,
如果是csv,则每个字段都用逗号分隔(默认情况下),您可以使用cut
选择所需的字段。
cut -d, -f8 files.csv | xargs -i cp {} /home/files/
You can read your CSV file line by line into a BASH array and use 8th element from array:
#!/bin/bash
while read -ra cols
do
cp "${cols[7]}" /home/files
done < files.csv
If csv file is comma delimited then use:
while IFS=, read -ra cols
do
cp "${cols[7]}" /home/files
done < files.csv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.