简体   繁体   中英

bash to copy file 3 times and rename each using lines in another file

In the bash below I am trying to copy the single text file in /home/cmccabe/QC/test/metrics.txt 3 times. Each one of the 3 text files is then renamed using lines 3-5 (always the same), from /home/cmccabe/QC/analysis.txt . However, I am getting an error and not sure how to fix it. Thank you :).

metrics.txt ( tab-delimited )

R_Index ISP Loading Pre-Enrichment  Total Reads Read Length Key Signal  Usable Sequence Enrichment  Polyclonal  Low Quality Test Fragment   Aligned Bases   Unaligned Bases Exception
1   89  .   78402052    201 77  61  98.6    29.2    10.4    79  98.8    1.2 

analysis.txt

status: complete
id names: 
00-0000_Last-First
01-0101_LastN-FirstN
02-0202_La-Fi

attempt

LinesToSkip=2
((StartLine=$LinesToSkip+1))

files=($(cat /home/cmccabe/QC/test/metrics.txt ))

i=1

while read -r new_name
do
mv "${files[$i]}" "$new_name"
((i=$i+1))
done < <(sed -n "${StartLine},\$p" /home/cmccabe/QC/analysis.txt)

mv: cannot stat ‘ISP’: No such file or directory
mv: cannot stat ‘Loading’: No such file or directory
mv: cannot stat ‘Pre-Enrichment’: No such file or directory

desired output (single column of data)

00-0000_Last-First_meterics.txt
01-0101_LastN-FirstN_metrics.txt
02-0202_La-Fi_metrics.txt

You foundm, that you can get the new filenames with sed .
Now you can get mv commands using printf:

printf "mv metrix.txt %s\n" "$(sed '1,2 d' /home/cmccabe/QC/test/metrics.txt)"

Now get the output executed like the were regular commands with <() .

source <(printf "mv metrix.txt %s\n" "$(sed '1,2 d' /home/cmccabe/QC/test/metrics.txt)")

source can be replaced by a dot and would like to see double quotes in the filename (might have a space in it):

. <(printf 'mv metrix.txt "%s"\n' "$(sed '1,2 d' /home/cmccabe/QC/test/metrics.txt)")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM