我一直在尝试根据表重命名一些特定文件,但没有成功。它要么重命名所有文件,要么给出错误。
该目录包含数百个以长条码命名的文件,我只想重命名包含模式 _1_ 的文件。
示例
barcode_1_barcode_SL484171.fastq.gz barcode_2_barcode_SL484171.fastq.gz barcode_1_barcode_SL484370.fastq.gz barcode_2_barcode_SL484370.fastq.gz
mytable.txt
旧名 | 新名字 |
---|---|
barcode_1_barcode_SL484171 | 说明1 |
barcode_2_barcode_SL484171 | 说明1 |
barcode_1_barcode_SL484370 | 说明2 |
barcode_2_barcode_SL484370 | 说明2 |
期望输出:
Description1.R1.fastq.gz Description2.R1.fastq.gz
正如您在表中看到的,每个描述有两个文件,但我只想用 _1_ 模式重命名那些文件。
我尝试过的代码:
for i in *_1_*.fastq.gz; do read oldname newname; mv "$oldname" "$newname".R1.fastq.gz; done < mytable.txt
for i in $(grep '_1_' mytable.txt); do read -r oldname newname; mv ${oldname} ${newname}.R1.fastq.gz; done < mytable.txt
for i in $(grep '_1_' mytable.txt); do oldname=$(cut -f1 $i);newname=$(cut -f2 $i); ln -s ${oldname} ${newname}.R1.fastq.gz; done
答案 0 :(得分:0)
while read -r oldname newname
do
if [[ $oldname =~ "_1_" ]]
then
mv $oldname $newname
fi
done < mytable.txt
答案 1 :(得分:0)
类似的东西。
#!/usr/bin/env bash
while IFS= read -r files; do ##: loop through the output of `grep 'barcode_1_barcode.*' table.txt`
while read -ru9 old_name prefix; do ##: loop through the output of `find . -name 'barcode_1_barcode*.gz' | grep -f <(cut -d' ' -f1 table.txt`
if [[ $files == *"$old_name"* ]]; then ##: If the filename from the output of find matches the first field of table.txt (space delimite)
old_filename="${files%.fastq.gz}" ##: Extract the filename without the fast.gz extesntion
extension="${files#"$old_filename"}" ##: Extract the extention .fast.gz without the filename
# mv -v "$files" "$prefix.R1${extension}"
printf '%s %s %s ==> %s\n' mv -v "$files" "$prefix.R1${extension}" ##: Rename the files to the desired output
fi
done 9< <(grep 'barcode_1_barcode.*' table.txt)
done < <(find . -name 'barcode_1_barcode*.gz' | grep -f <(cut -d' ' -f1 table.txt) ) ##: Remain the first column/field of table.txt
来自 OP 示例数据/文件的输出。
renamed './barcode_1_barcode_SL484370.fastq.gz' -> 'Description2.R1.fastq.gz'
renamed './barcode_1_barcode_SL484171.fastq.gz' -> 'Description1.R1.fastq.gz'
如果您对输出感到满意,请将 #
从 mv
的前面移到
printf
的前面或者只是用 printf
删除整行并从
#
mv
以便 mv
实际重命名文件。