Bash to copy file 3 times and rename based on another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to copy file 3 times and rename based on another file
# 1  
Old 02-22-2017
Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you Smilie.


/home/cmccabe/Desktop/test/list.txt
Code:
analysis done using v1.5 by cmccabe at 02/08/17 12:19:08 PM
patients analyzed: 
00-0000_Last-First
11-1111_LastName-FirstName
22-2222_LastLastName-FirstFirstName

/home/cmccabe/Desktop/list/QC (always a single text file with data in it)
Code:
metrics.txt


Code:
linesToSkip=2
{
    for ((i=$linesToSkip;i--;)) ;do
        read
        done
files=( /home/cmccabe/Desktop/list/QC/metrics.txt )
i=0
while read -r new_name; do
  mv "${files[$i]}" "$new_name"
  (( i++ ))
}
done < /home/cmccabe/Desktop/test/list.txt


Last edited by cmccabe; 02-22-2017 at 09:14 AM.. Reason: fixed format
# 2  
Old 02-22-2017
You might be better with something like this:-
Code:
LinesToSkip=2
((StartLine=$LinesToSkip+1))

files=($(cat /home/cmccabe/Desktop/list/QC/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/Desktop/test/list.txt

It uses sed to get the whole file from after the LinesToSkip value. It also assumes that your list of files to rename is a white-space and/or row separated input file.

You may prefer to increment the counter in a different way, but the principle is the same.


I hope that this helps,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 02-22-2017
As long as you're using bash (or a 1993 or later version of ksh) and not just restricting yourself to POSIX required features and assuming that the file /home/cmccabe/Desktop/list/QC/metrics.txt contains the names of the files you want to move on the 1st three lines of that file (rather than being the name of a file that you want to copy to three other files), I'd be tempted to replace:
Code:
linesToSkip=2
{
    for ((i=$linesToSkip;i--;)) ;do
        read
        done
files=( /home/cmccabe/Desktop/list/QC/metrics.txt )
i=0
while read -r new_name; do
  mv "${files[$i]}" "$new_name"
  (( i++ ))
}
done < /home/cmccabe/Desktop/test/list.txt

with:
Code:
linesToSkip=2
{	for((i = 1; i <= linesToSkip; i++))
	do	read
	done

	while read -r new_name && read -ru4 old_name
	do	mv "$old_name" "$new_name"
	done
} < /home/cmccabe/Desktop/test/list.txt 4< /home/cmccabe/Desktop/list/QC/metrics.txt


Last edited by Don Cragun; 02-22-2017 at 04:24 PM.. Reason: Remove the echo.
These 3 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 02-23-2017
A much neater solution there without needlessly creating and stepping through an array. Would you mind if I borrowed it? Smilie


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 02-23-2017
Quote:
Originally Posted by rbatte1
A much neater solution there without needlessly creating and stepping through an array. Would you mind if I borrowed it? Smilie


Kind regards,
Robin
Hi Robin,
Would I mind? Absolutely not! If you learn something from one of my posts, I hope you (and other readers) will learn from it and be able to use it in scripts you write. That is why I post in this forum. Smilie
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 02-23-2017
Thank you both for the help and helping learn. Very useful and informative Smilie.
# 7  
Old 03-14-2017
@Don Cragun
I am trying to understand your code and thought I did but after it executes there is no output. I only modified the last line as I thought that was why there was no output, but it appears I was mistaken. I also tried to add the "_" (in bold). Thank you Smilie.

Code:
linesToSkip=2   # skip lines 1 and 2
{	for((i = 1; i <= linesToSkip; i++))   # start at line 3
	do	read   # read lines 3,4,5
	done   # stop

	while read -r new_name && %s\"_"\n && read -ru4 old_name   # read metrics.txt in variable
	do	mv "$old_name" "$new_name"   copy orignal metrics.txt 3 times
	done   # stop
} < /home/cmccabe/Desktop/QC/test/metrics.txt 4< /home/cmccabe/Desktop/QC/analysis.txt   # rename each metrics.txt using lines 3,4,5 of analysis.txt

/home/cmccabe/Desktop/QC/test/metrics.txt - tab-delimeted, always two lines - this is the file that is copied 3 times and renamed always using lines 3,4,5 of /home/cmccabe/Desktop/QC/analysis.txt

Code:
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

/home/cmccabe/Desktop/QC/analysis.txt

Code:
analysis done using v1.5 by cmccabe at 02/08/17 12:19:08 PM
patients analyzed:
00-0000_Last-First
11-1111_LastName-FirstName
22-2222_LastLastName-FirstFirstName

desired output in /home/cmccabe/Desktop/QC/test

Code:
00-0000_Last-First_metrics.txt
11-1111_LastName-FirstName_metrics.txt
22-2222_LastLastName-FirstFirstName_metrics.txt


Last edited by cmccabe; 03-14-2017 at 10:12 AM.. Reason: added code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

File Copy based on file receive date

I have bunch of files in my source folder like below and every day based on date I am receiving file ex: Nov 28,Nov 29,Nov 30 ... -rw-rw-r--+ 1 root root 20 Nov 27 06:00 aaaa27.txt -rw-rw-r--+ 1 root root 20 Nov 28 06:00 aaaa28.txt -rw-rw-r--+ 1 root root 20 Nov 29 06:00 aaaa29.txt I... (4 Replies)
Discussion started by: krish2014
4 Replies

3. Shell Programming and Scripting

Need help in writitng a script to rename file name and copy to other folder

Hi All, My requirement is as follows: A file (say abc) will be having list of the .txt file names. I need to read this abc file line by line and rename the .txt file names inside it and move them to other folder/path. Eg: abc ------- file1.txt file2.txt file3.txt Output (should... (1 Reply)
Discussion started by: pavan.yadalla
1 Replies

4. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

5. Shell Programming and Scripting

Rename portion of file based on another file

Hello, I've been searching and reading, but I can't figure out how to solve this problem with my newbie skills. In my directory, I have a list of files (see dirlist.txt attachment) that I need to merge and rename. I have part of the code of the code figured out (see below). However, I... (3 Replies)
Discussion started by: anjulka
3 Replies

6. Shell Programming and Scripting

.sh file To rename existing file and copy new file

Hi All, I am very new to shell scripting . In my current task i want to create .sh file that will rename the existing file with appending _bu in it. And then copy new file . e.g if i have file linuxFirst.java then i want to rename it to linuxFirst_bu.java ..Then want replace with latest... (1 Reply)
Discussion started by: maheshkaranjkar
1 Replies

7. Shell Programming and Scripting

copy and rename file..

hi all, I have one config folder and updates folder.updates folder contains file tmp_2.0_20201208_45.xml and config folder contains file tmp.xml.When the tmp_2.0_20201208_45.xml file is copied in the config folder the name of this file is changed in config folder again as tmp.xml(old... (8 Replies)
Discussion started by: shubhig15
8 Replies

8. Shell Programming and Scripting

Copy 1 file 5000 times and Rename each +1

Hi, Found lots of stuff that is close but no cigar... I have a file ie. a.txt, and I want to copy it to another directory 5000 times and call it: a1.txt a2.txt ... a5000.txt Struggling to put a loop together in this new world of AIX. please include full script for me to understand... (3 Replies)
Discussion started by: terry2009
3 Replies

9. Shell Programming and Scripting

copy/rename file as date() unix/shell

File.jpg I want to copy and rename this as 2008-12-02.jpg I tried this copy File.jpg date '%y-%m-%d-%H:%M:%S'.jpg This doesnt work.... what do i do? (1 Reply)
Discussion started by: hdogg
1 Replies

10. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies
Login or Register to Ask a Question