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
# 8  
Old 03-17-2017
I sincerely apologize for misleading you with my script. I paid too much attention to the code sample you provided in post #1 in this thread and not enough attention to the English description (which I found confusing). The description talks about copying files (and wasn't clear whether there was one file being copied or one file that contained the names of files to be copied), while your code was moving files whose source file names seemed to appear in one file and whose destination names seemed to appear in a second file. The fact that your code was moving (using mv) and not copying (using cp) seemed to confirm that there were three source files instead of one. (If there is only one source file, the last two copies will obviously fail since you're using mv instead of cp!)

If you have a single source file, and you're reading a destination name from a file, but you don't want the name of that destination file to be the name you read from the file; you need to clearly explain the transformation that is supposed to take place. (There is nothing in any of your code that makes any attempt to modify the names read from the file. And, I haven't found anything in your descriptions that talks about modifying the names found in the file other than confusing examples that don't clearly explain where the names are coming from.)

The code that you showed us (that you said produces no output) should be producing a diagnostic message because it is trying to invoke a utility named
Code:
%s"_\n && read -ru4 old_name   # read metrics.txt in variable
	do	mv old_name    copy orignal metrics.txt 3 times
	done   # stop... ... ...

continuing on until a matching double-quote character (") is found to match the double-quote character that appears at the end of $new_name". Note that the new_name variable is not defined in your modification to my code (and therefore, the expansion $new_name expands to an empty string).

Please give us a clearer description of what you are trying to do.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 03-18-2017
The metrics.txt in /home/cmccabe/Desktop/QC/test is the file that is copied 3 times. Each one of of those 3 copies is then renamed using lines 3,4,5 in analysis.txt in /home/cmccabe/Desktop/QC with a _ in the name.

So in /home/cmccabe/Desktop/QC/test before the code is executed there is one metrics.txt. Then after the code is executed there are 3 metrics.txt that are renamed using the analysis.txt, lines 3,4,5.

The format of analysis.txt is always the same, that is it is always five lines with the first two being skipped with lines 3,4,5 having the rename lines in them.

analysis.txt in /home/cmccabe/Desktop/QC
Code:
status: complete
id names: 
00-0000_Last-First
01-0101_LastN-FirstN
02-0202_La-Fi

metrics.txt in /home/cmccabe/Desktop/QC/test, then code is executed:

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

Does this help and thank you Smilie.

Last edited by cmccabe; 03-18-2017 at 11:55 AM.. Reason: fixed format
# 10  
Old 03-18-2017
Quote:
Originally Posted by cmccabe
The metrics.txt in /home/cmccabe/Desktop/QC/test is the file that is copied 3 times. Each one of of those 3 copies is then renamed using lines 3,4,5 in analysis.txt in /home/cmccabe/Desktop/QC with a _ in the name.

So in /home/cmccabe/Desktop/QC/test before the code is executed there is one metrics.txt. Then after the code is executed there are 3 metrics.txt that are renamed using the analysis.txt, lines 3,4,5.

The format of analysis.txt is always the same, that is it is always five lines with the first two being skipped with lines 3,4,5 having the rename lines in them.

analysis.txt in /home/cmccabe/Desktop/QC
Code:
status: complete
id names: 
00-0000_Last-First
01-0101_LastN-FirstN
02-0202_La-Fi

metrics.txt in /home/cmccabe/Desktop/QC/test, then code is executed:

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

Does this help and thank you Smilie.
According to the above description, we are supposed to start with two files in a directory: metrics.txt and analysis.txt. Then we are supposed to create three more copies of metrics.txt also named metrics.txt and then we are supposed to rename three of those four file and end up with four files: 00-0000_Last-First_metrics.txt, 11-1111_LastName-FirstName_metrics.txt, 22-2222_LastLastName-FirstFirstName_metrics.txt, and analysis.txt.

Doing it in this sequence is impossible because you can never have two files in one directory with the same name. We can copy a file three times to new files with different names. But, if we do that, the original metrics.txt file would still be there. (But your description above says it should not be there.) So, is what you really want correctly described by saying that you want to rename metrics.txt to be the filename derives from the third line in analysis.txt and then you want to copy that file to two more files with filenames derived from the 4th and 5th lines in analysis.txt?

Why do you need three or four copies of the same text? Why not just link the three new names to metrics.txt using an ln command (or tell whatever program will be reading these files that the name of the file they should read is just metrics.txt)?
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 03-20-2017
The metrics.txt in /home/cmccabe/Desktop/QC/test is saved as 3 separate .txt files. Each one of of those 3 .txt files is then renamed using lines 3,4,5 in analysis.txt in /home/cmccabe/Desktop/QC with a _ in the name.

Another process that is used is expecting 3 files in home/cmccabe/Desktop/QC/test to function properly, so this is a temporary solution to that (until that process can be re-written). Thank you very much Smilie.

home/cmccabe/Desktop/QC/test

Code:
metrics1.txt
metrics2.txt
metrics3.txt

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

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

desired output in home/cmccabe/Desktop/QC/test after the script executes

Code:
00-0000_Last-First_metrics1.txt
11-1111_LastName-FirstName_metrics2.txt
22-2222_LastLastName-FirstFirstName_metrics3.txt

Since there is nothing to match between the two file I don't thin process substitution can be used, the names are just:

Code:
metrics1.txt = line 3 of analysis.txt
metrics2.txt = line 4 of analysis.txt
metrics3.txt = line 5 of analysis.txt


Last edited by cmccabe; 03-20-2017 at 09:11 AM.. Reason: fixed format
# 12  
Old 03-20-2017
Hi.

Extraction of base filenames, then tack on the suffix:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate replicate file to files determined by list.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sed cp

pl " Input data files data1, data2:"
head data[12]

pl " Results:"
for file in $( sed -n '3,5p' data2 )
do
  echo cp data1 ${file}_metrics.txt
done

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
sed (GNU sed) 4.2.2
cp (GNU coreutils) 8.23

-----
 Input data files data1, data2:
==> data1 <==
Some text that is not important to the process.

==> data2 <==
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

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

Remove the final echo when satisfied that the filenames are correct.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 13  
Old 03-28-2017
Thank you very much Smilie
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