Bash to rename files repeats previous filename in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to rename files repeats previous filename in directory
# 1  
Old 03-25-2017
Bash to rename files repeats previous filename in directory

In the below bash processes substitution, if there are 3 files in a directory /home/cmccabe/medex.logs/analysis.log, the filename variable is set to where these files are located.

The code does execute, the problem is that if there is a renamed file in the output directory below, it gets repeated but the new one does not.

So,lets say that there is a file in the output directory that has been renamed to
Code:
00-0000_Last-First_fbn1_20xcoverage.txt

when the script executes the second time the output directory looks like:
Code:
00-0000_Last-First_fbn1_20xcoverage_Last-First_fbn1_20xcoverage.txt
01-0101_LastN-FirstN_fbn1_20xcoverage.txt

the renamed file in the directory repeats while the new file being renamed is ok.

However if I execute the script a third time the directory looks like:
Code:
00-0000_Last-First_fbn1_20xcoverage_Last-First_fbn1_20xcoverage.txt
01-0101_LastN-FirstN_fbn1_20xcoverage_fbn1_20xcoverage.txt
02-0202_La-Fi_fbn1_20xcoverage.txt

I am not sure why the filenames repeat for the previous and do not know how to fix it. Thank you Smilie.

Code:
# declare associative array
declare -A mapArray

# Set variable
filename=$(awk 'END{print}' /home/cmccabe/medex.logs/analysis.log)

# Read the file from the 3rd line of the file and create a hash-map
while IFS= read -r  line; do
    line="$line"
    mapArray["${line%_*}"]="$line"
done < <(tail -n +3 /home/cmccabe/Desktop/NGS/API/$filename/analysis.txt)

# construct hash map and rename the text file
for file in *.txt; do
    echo "$file" ${mapArray["${file%%_*}"]}"_${file#*_}"
    # mv "$file" ${mapArray["${file%%_*}"]}"_${file#*_}"
done

text file in /home/cmccabe/Desktop/percent - there could be a maximum of 3 files in this directory
Code:
00-0000_fbn1_20xcoverage.txt
01-0101_fbn1_20xcoverage.txt
02-0202_fbn1_20xcoverage.txt

text file in /home/cmccabe/Desktop/analysis.txt

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

desired result in /home/cmccabe/Desktop/percent

Code:
00-0000_Last-First_fbn1_20xcoverage.txt
01-0101_Last-First_fbn1_20xcoverage.txt
02-0202_Last-First_fbn1_20xcoverage.txt

I think it may be the for file in *.txt; do line that is causing the repeats. If there is a renamed file in the directory and a new one is executed then the old file gets renamed as well as the new file.
So if there is a renamed file in the directory
Code:
00-0000_Last-First_fbn1_20xcoverage.txt

already and a new file gets renamed to 01-0101_Last-First_fbn1_20xcoverage.txt, the original file is repeated in because of that for. I think that is he issue but not sure how to fix it. Thank you Smilie.

I added line="$line" under thewhile and using set -x, can see the original file as well as the new file are read into line, but not sure how to only process the new one.

Code:
+ read -r line
+ line=01-0101_LastN-FirstN    --- not part of set -x but this is the original file that is already renamed
+ mapArray["${line%_*}"]=01-0101_LastN-FirstN
+ IFS=
+ read -r line
+ line=02-0202_La-Fi      --- not part of set -x but this is the new file to be renamed
+ mapArray["${line%_*}"]=02-0202_La-Fi
+ IFS=
+ read -r line
+ for file in '*.txt'
+ mv 00-0000_Last-First_fbn1.txt 00-0000_Last-First_Last-First_fbn1.txt
+ for file in '*.txt'
+ mv 02-0202_fbn1.txt 02-0202_La-Fi_fbn1.txt


Last edited by cmccabe; 03-27-2017 at 05:59 PM.. Reason: fixed format, updated post
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to calculate average of all files in directory and output by part of filename

I am trying to use awk to calculate the average of all lines in $2 for every file in a directory. The below bash seems to do that, but I cannot figure out how to capture the string before the _ as the output file name and have it be tab-delimeted. Thank you :). Filenames in... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

Rename files in a directory and move them

I have a directory e2e_ms_xfer/cent01 this contains the multiple files some of which will be named below with unique date time stamps e2e_ms_edd_nom_CCYYMMDD_HHMM.csv What I want to do is in a loop 1) Get the oldest file 2) Rename 3) Move it up one level from e2e_ms_xfer/cent01 to... (1 Reply)
Discussion started by: andymay
1 Replies

3. Shell Programming and Scripting

Rename all files (filename with spaces) to different extension

Hi, I have files with filenames as below. SGM Daily Sales Email-en-us-05312012.xlwa I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders. I have the following script. #!/bin/ksh for oldfile in $(find... (1 Reply)
Discussion started by: mac4rfree
1 Replies

4. UNIX for Dummies Questions & Answers

Bash script to rename files in a directory

Dear friends, I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It... (4 Replies)
Discussion started by: djsnifer
4 Replies

5. Shell Programming and Scripting

Rename FileName in the Directory

In the Directory all the Files are following format. Filename_yyyymmdd_numbers.txt eg. file_name_20120106_015802.txt . I want to write the Shell script to rename all the file to file_name.txt.in the directory. Thanks Mani (5 Replies)
Discussion started by: gavemani
5 Replies

6. Shell Programming and Scripting

How do I rename a filename in a directory?

Hi, I've got a large to task to do, which I've broken into three section. I'm just stuck on one of the sections. I have to change the end of a filename from .txt to .doc in a directory. So if I have a directory called "folder1" and two files contained in it called "file1.txt" and "file2.txt",... (7 Replies)
Discussion started by: TeddyP
7 Replies

7. Shell Programming and Scripting

Rename many files in a directory

Hi, I have around 100 xml file in a directory. I need to rename the files from .xml to .xml1. So i tried using the following command: mv *.xml *.xml1 but i am getting the following error mv: when moving multiple files, last argument must be a directory Try `mv --help' for more... (8 Replies)
Discussion started by: ananthi_ku
8 Replies

8. Shell Programming and Scripting

how to rename all files that have a certain text in the filename using tcsh shell

Hello~ I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out. i.e. original filenames: ... (3 Replies)
Discussion started by: in2vtec
3 Replies

9. Shell Programming and Scripting

Simple BASH shell script to rename webcam jpg and copy into a new directory.

System: Ubuntu Intrepid Ibex I'm running webcamd as a sort of "security" program, but I need a script that will archive my webcam.jpg files. So, take the following file: /home/slag/www/webcam.jpg Rename it--preferably with a time stamp. Place it in say: /home/slag/www/history/ ... (4 Replies)
Discussion started by: robfindlay
4 Replies
Login or Register to Ask a Question