justaguy
justaguy

Reputation: 3022

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

Upvotes: 0

Views: 53

Answers (1)

Walter A
Walter A

Reputation: 20022

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)")

Upvotes: 1

Related Questions