Issue
I'm working in a directory in which I have a lot (about 100) of BAM file with a specific name. In the same directory I have also a csv file containing 4 column: FileID, File.name, Donor, Type
Let's say that my bam files in the directory are: Donor.1234-xyz.bam, Donor.5678-abc.bam, Donor.1011-def.bam, Donor.1213-ghi.bam
(which match with the name in the column number 2 called File.name of the csv file).
I am not really familiar with coding so I will try to explain what I would like to do. I would like the script to rename the bam files I have in the folder with the name Donor+Type (column 3 and 4). So if Donor.1234-xyz.bam is also found in the File.name column of the csv file then I would like it to be called with the string in column 3 and 4 (basically I would like to replace the bam name with everything in the corresponding column of the Donor and the Type).
BAM_FILE="*.bam"
BAM=$BAM_FILE
NAME="cat kich.csv | cut -f2 -s"
NM=$NAME
DONOR="cat kich.csv | cut -f3 -s"
DO=$DONOR
TYPE="cat kich.csv | cut -f4 -s"
TY=$TYPE
for file_name in "$BAM";
do
if [[ "$file_name" == "$NM" ]] then
mv ${file_name} ${DO}_${TY} ;
done
But it doesn't work really, as I said I'm still naïve. So, could you help me please to fix this problem?
Solution
Assuming your file is something like that:
$ cat file.csv
01;file01;part1;part2
02;file02;part3;part4
You can try it
while read -r line;
do
file=$(echo $line | cut -d";" -f2)
newName=$(echo $line | cut -d";" -f3)_$(echo $line | cut -d";" -f4)
mv $file $newName
done < file.csv
Answered By - Juranir Santos Answer Checked By - Katrina (WPSolving Volunteer)