Renaming files by matching info from a separate file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming files by matching info from a separate file
# 1  
Old 04-28-2010
Renaming files by matching info from a separate file

Hi All,

I could use a bit of help with this as I'm at a loss.

I have a number of files all named accordingly:

Quote:
032210.mp3
032210.mp4
032210.flv
032310.mp3
032310.mp4
032310.flv
etc.
I have a separate text file that is as follows:

Quote:
MON 032210 3629
TUE 032310 3658
WED 032410 3616
THU 032510 3651
FRI 032610 3675
What I want to do is end up with:

Quote:
032210-003629.mp3
032210-003629.mp4
032210-003629.flv
032310-003658.mp3
032310-003658.mp4
032310-003658.flv
etc.
I don't know how to do this, though I'm certain it can be done, and would rather learn how than do it all manually. If someone could just point me in the right direction or post the basics so that I could have a starting point I'd much appreciate it.

So far all I've managed to do is replace the ^M from the text file with \r so that cat works properly on it.

System is OS X server, in case it makes a difference.

Thanks.
# 2  
Old 04-29-2010
What is the logic that transforms what you have into what you want?
# 3  
Old 04-29-2010
Quote:
Originally Posted by cfajohnson
What is the logic that transforms what you have into what you want?
As best as I can tell (from what I've seen in google results at least) it would be something like the following (yes, I know I'm missing stuff):

ls -ltr | awk '{print $9}'

for $line compare to cat episodes where $line matches cat episodes | awk '{print $2}' mv $line $line-00'{print $3}'.*

That's what I think of anyways. As you can tell I'm missing a bit of the structure, and I have the print 2 and 3 needing input from the second awk, not the first, and I'm unsure of how to get the data from the first added into the second part of it.
# 4  
Old 04-29-2010
Try...
Code:
ls -tr ??????.??? | gawk 'FNR==NR{a[$2]=$3;next}{printf("mv %s %s-%06d.%s\n",$0,$1,a[$1],$2)}' textfile FS="." - | sh

I would leave out the "| sh" at the end until you are happy with the results.
# 5  
Old 04-29-2010
Or something like
Code:
#!/bin/bash
for F in *.*
do	mv $F "${F%.*}-00$(grep ${F%.*} file | cut -d' ' -f3).${F#*.}"
done

# 6  
Old 04-29-2010
Quote:
Originally Posted by Ygor
Try...
Code:
ls -tr ??????.??? | gawk  'FNR==NR{a[$2]=$3;next}{printf("mv %s %s-%06d.%s\n",$0,$1,a[$1],$2)}'  textfile FS="." - | sh

I would leave out the "| sh" at the end until you are happy with the results.
I installed gawk, as it's not available by default on OS X, though I didn't notice any difference between running that command with awk vs gawk.

When I run it without the "| sh" I get the following:

Code:
admin$ ls -tr *.* | gawk 'FNR==NR{a[$2]=$3;next}{printf("mv %s %s-%06d.%s\n",$0,$1,a[$1],$2)}' episodes FS="." -

mv bw042810.mp4 bw042810-000000.mp4
...

Quote:
Originally Posted by frans
Or something like
Code:
#!/bin/bash
for F in *.*
do    mv $F "${F%.*}-00$(grep ${F%.*} file | cut -d' ' -f3).${F#*.}"
done

When I run this (having changed "file" to "episodes" which is the file I showed in my original post) I get the following results:

Code:
bw042810-00.mp4

I appreciate your help, for whatever reason it seems both methods are not getting the input from the episodes file correctly. I've checked but I'm unsure what I should be doing differently do get it to work.
# 7  
Old 04-29-2010
The problem should come from the formatting of the 'episodes' file. Is it Tab, single space or whatever else separated ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print the overlapping entries in 2 files to separate file

I have two files that contain overlapping positions. i want to put them together each overlapping entries in both files in to a new file (the entries of first file first and the entries of second file next) followed by blank line then next overlapping entries and so on. input1 chr1 22 ... (10 Replies)
Discussion started by: raj_k
10 Replies

2. Shell Programming and Scripting

Renaming the file name for n number of files

Hi , I am kind of new to shell scripting and found a situation to handle ... I have few files which will be ftpd in to our sustem , the file names needs to be renamed based on condition. ------------ Eg file names :- AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT... (6 Replies)
Discussion started by: chillblue
6 Replies

3. Shell Programming and Scripting

grepping files and then renaming file

Hi, What is the easiest way to list a directory with 1000s of filenames, grep it for a certain sequence of numbers, and if found to rename the file by the value you are grepping. eg The file I am examining will looks like this: 1234 1224343 2324 244 35665 If I am examining a list... (1 Reply)
Discussion started by: mantis
1 Replies

4. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

5. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

6. Shell Programming and Scripting

matching and extracting info from text files

Hi all, I have two .txt file i.e. First text file: 2 4 1 4 Second text file 2 1.nii.gz 4 334.nii.gz 1 12.nii.gz 4 134.nii.gz If entry in 1st column of 1st text file matches the 1st column of 2nd text file, then copy the file (name of which is the second column) associated with... (4 Replies)
Discussion started by: vd24
4 Replies

7. UNIX for Dummies Questions & Answers

Renaming files in one file from names in other

Hi Guys, I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like K2_34625-34675 K7_988963-988983 K12_773882-7734102 and the other set corresponding to the same is U_P_321_9_3_11.ab1 U_P_322_9_3_11.ab1 U_P_323_9_3_11.ab1 Now... (23 Replies)
Discussion started by: pawannoel
23 Replies

8. Shell Programming and Scripting

Splitting text file into 2 separate files ??

Hi All, I am new to this forumn as well to the UNIX, I have basic knowledge of UNIX which I studied some years ago, now I have to do some shell scripting to load data into Oracle database using sqlldr utility, whcih I am able to do. I have a requirement where I need to do following operation. I... (10 Replies)
Discussion started by: shekharjchandra
10 Replies

9. Shell Programming and Scripting

Matching a string (zip code) from a list in a separate file

I have a list of postal addresses and I need to pull the records that match a list of zip codes in a separate file. The postal addresses are fixed width. The zip code is located in character position 149-157. Something better than: cat postalfile.txt | grep -f zipcodes.txt would be great. $... (8 Replies)
Discussion started by: sitney
8 Replies

10. Shell Programming and Scripting

Break a file into separate files

Hello I am facing a scenario where I have a file with XML content and I am running shell script over it. But the problem is the XML is getting updated with new services. In the below scenario, my script takes values from the xml file from one service name say ABCD. Since there are multiple, it is... (8 Replies)
Discussion started by: chiru_h
8 Replies
Login or Register to Ask a Question