Rename files based on simple text file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Rename files based on simple text file
# 1  
Old 01-16-2017
Rename files based on simple text file

Hello!

New here although not completely new to Unix.
I wonder how I could rename files based on the data found in a simple textfile.
It goes like this:

I have 4 files
Code:
1 ldfgkkfjslkdfjsldkfjsf.wav
2 nndsdflksdjf.wav
3 sdflksjdf jjsdflsdfl.wav
4 dkadsdddd.wav

Textfile.txt looks like this:
Code:
1 ^t Some ^t Data
2 ^t Other ^t Info & Stuff
3 ^t Yet ^t More Data
4 ^t To the ^t End

^t being tab, or some other apropriate separation character

I want files to look like this:
Code:
01 - Some -Data
02 - Other - Info & Stuff
03 - Yet - More Data
04 - To the - End

I realize there's a problem if the text document does not match the number of files but that does not need to be error handled. It does not need to be fool proof, I will do this one folder at a time, checking for errors manually. Each folder contains some 10-30 files. I want leading zeroes up to 9 if possible but it's not extremely important.

Background: These are mediafiles converted from tape archives. The titles are in a separate textfile for each converted tape. Each tapes goes into a separate folder.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-16-2017 at 07:53 AM.. Reason: Added CODE tags.
# 2  
Old 01-16-2017
Not sure I fully understand.
You want to rename several ".wav" files in a directory - not more than 30, so definitely NOT 3 digits - to new names without the ".wav" extension based on the contents of a text file indexed by the leading number in the ".wav" file name?

Any attempts / ideas / thoughts from your side? Any preferred tools?

Last edited by RudiC; 01-16-2017 at 05:27 PM.. Reason: typo
# 3  
Old 01-20-2017
Quote:
Originally Posted by RudiC
Not sure I fully understand.
You want to rename several ".wav" files in a directory - not more than 30, so definitely NOT 3 digits - to new names without the ".wav" extension based on the contents of a text file indexed by the leading number in the ".wav" file name?

Any attempts / ideas / thoughts from your side? Any preferred tools?
Sorry for late answer, been busy.

Yes, I think you got it just right.
The file with index 1 get's it name from line 1 in the document.
The file with index 23 get's it name from line 23 in the document.
Each line begins with a digit already and these should in almost all cases coincide with the line number. No file exceeds 99 lines.

I read the manual for grep and also had a look at commands like sed and I know I can pipe the output of one command to the input of another command but I can't figure out how to do that in this case. In pseudocode it would be something like:
Code:
Start
n = 1
While not EOF in document.txt
A = contens of line n in document.txt
rename file n*.wav to A
end while

If it's possible I would rather do this with a simple commad in Unix rather than build a script. It's not meant to be advanced in any way, handling exceptions and things like that. If it works 8 times out of 10 I'm fine.

---------- Post updated at 08:57 PM ---------- Previous update was at 08:47 PM ----------

In this post there's a similar question but I don't have the corresponding filename in the document so i can't use that although it's quite similar.
Rename files based on name in text file
# 4  
Old 01-20-2017
Why are the filename and message content in different files? One little mistake will create a giant mess.

In any case, this being UNIX, there's ways to hedge your bets... I'd make an 'out' folder and create links in it, instead of renaming the original files. If something goes wrong, you're not left with all your files irreparably misnamed, just a bit of trash you can delete and start over.

Code:
#!/bin/bash
mkdir -p ./out/

exec 5<filenames
exec 6<filetitles
while read LINE FILENAME <&5 && IFS=$'\t' read NUM FILETITLE <&6
do
        if [ "$NUM" -ne "$LINE"]
        then
                echo "Numbers do not match?"
                continue
        fi

        FILETITLE="${FILETITLE//$'\t'/ - }" # Change tab into ' - '        
        ln "$FILENAME" out/"${NUM} ${FILETITLE}"
done

exec 5<&- # Close files
exec 6<&-

This should create links in the ./out/ folder of the right names.
# 5  
Old 01-20-2017
Try also
Code:
while IFS=" " read Nr NFN
  do    NZ="0$Nr"
        echo mv "$Nr "*".wav" "${NZ#${NZ%??}} ${NFN// /-}"
  done < file

Remove the echo command if happy with the proposals.
# 6  
Old 01-20-2017
Quote:
Originally Posted by Corona688
Why are the filename and message content in different files? One little mistake will create a giant mess.

In any case, this being UNIX, there's ways to hedge your bets... I'd make an 'out' folder and create links in it, instead of renaming the original files. If something goes wrong, you're not left with all your files irreparably misnamed, just a bit of trash you can delete and start over.

Code:
#!/bin/bash
mkdir -p ./out/

exec 5<filenames
exec 6<filetitles
while read LINE FILENAME <&5 && IFS=$'\t' read NUM FILETITLE <&6
do
        if [ "$NUM" -ne "$LINE"]
        then
                echo "Numbers do not match?"
                continue
        fi

        FILETITLE="${FILETITLE//$'\t'/ - }" # Change tab into ' - '        
        ln "$FILENAME" out/"${NUM} ${FILETITLE}"
done

exec 5<&- # Close files
exec 6<&-

This should create links in the ./out/ folder of the right names.

Thanks, the files originate from an old audio tape archive which at one point has been digitized. The textfiles where created a long time ago on a separate system.

I will have a look at your script – thank you.

---------- Post updated at 09:28 PM ---------- Previous update was at 09:10 PM ----------

Quote:
Originally Posted by RudiC
Try also
Code:
while IFS=" " read Nr NFN
  do    NZ="0$Nr"
        echo mv "$Nr "*".wav" "${NZ#${NZ%??}} ${NFN// /-}"
  done < file

Remove the echo command if happy with the proposals.
That seems to work about right.
I have two problems though.
The filenames looks like this:
03 ELO---Happy-Haunting

But I would prefer:
03 - ELO - Happy Haunting

I need some sort of separation between the thre fields which are track number, artist, title.
If blanks needs to be converted then perhaps underscore is better? Like: " 02_-_artist_-_title_song.wav"
I could easily convert underscore back to space afterwards.

Is this the section where blanks are converted?
Quote:
${NFN// /-}
Also, it seems like some folders have their files numbered like this:
01 fsfsdf.wav
02 dfgh ert.wav
...
11 abc def.wav

Leading zeroes below ten. Where do I modify the script for this?

Last edited by Oortone; 01-20-2017 at 04:47 PM..
# 7  
Old 01-20-2017
Does this mean you do or don't want leading zeroes?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

2. UNIX for Dummies Questions & Answers

Rename files based on a list

Hi, I have a directory with a lot of files like this: a.bam b.bam c.bam I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file: a x b y c ... (4 Replies)
Discussion started by: a_bahreini
4 Replies

3. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

4. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

5. UNIX for Dummies Questions & Answers

rename files based on their respective directory name

I have a number of files in directories labeled like this: /Data/tr_gray/tr_DTI/dti_FA.nii.gz (the brackets here represent a range of number that the files are labeled with) I need to rename each dti_FA.nii.gz file according to the name of the folder it resides in. For example, the file ... (3 Replies)
Discussion started by: tk0034
3 Replies

6. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

7. Shell Programming and Scripting

Bash snippet to find files based on a text file?

Evening all. I'm having a terrible time with a script I've been working on for a few days now... Say I have a text file named top10song.tm2, with the following in it: kernkraft 400 Imagine i kissed a girl Thriller animals hallelujah paint it black psychosocial Oi to the world... (14 Replies)
Discussion started by: DJ Charlie
14 Replies

8. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

9. Shell Programming and Scripting

splitting files based on text in the file

I need to split a file based on certain context inside the file. Is there a unix command that can do this? I have looked into split and csplit but it does not seem like those would work because I need to split this file based on certain text. The file has multiple records and I need to split this... (1 Reply)
Discussion started by: matrix1067
1 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question