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
# 8  
Old 04-29-2010
Quote:
Originally Posted by frans
The problem should come from the formatting of the 'episodes' file. Is it Tab, single space or whatever else separated ?
Tab separated.

I hadn't thought of that affecting it as my tests with awk always seemed to show the right data... Should I be converting it to comma or single space or what?

How can I tell from those scripts which format they are looking for? (so I can always know in the future).

Thanks.

Edit: I have since attempted this with comma and single space delimiters to the exact same effect. Is there something else in the formatting I should be looking at? I've just been doing a simple: :%s/^I/ /g and the like.

Last edited by Demosthenes; 04-29-2010 at 05:10 AM..
# 9  
Old 04-29-2010
the just remove the -d' ' (delimiter=SPACE) from the line :
Code:
#!/bin/bash
for F in *.*
do	mv $F "${F%.*}-00$(grep ${F%.*} episodes | cut -f3).${F#*.}"
done

# 10  
Old 04-29-2010
Quote:
Originally Posted by frans
the just remove the -d' ' (delimiter=SPACE) from the line :
Code:
#!/bin/bash
for F in *.*
do    mv $F "${F%.*}-00$(grep ${F%.*} episodes | cut -f3).${F#*.}"
done

This still gives the same result. I also attempted with a space and comma delimited file, to the same effect.
# 11  
Old 04-29-2010
Let's debug (add -x on first line)
Code:
#!/bin/bash -x
for F in *.*
do    mv $F "${F%.*}-00$(grep ${F%.*} episodes | cut -f3).${F#*.}"
done

# 12  
Old 04-29-2010
Quote:
Originally Posted by frans
Let's debug (add -x on first line)
Code:
#!/bin/bash -x
for F in *.*
do    mv $F "${F%.*}-00$(grep ${F%.*} episodes | cut -f3).${F#*.}"
done

Code:
$ ls
bw032210.mp4    bw042810.mp4    episodes    episodes-bak    script
$ cat script 
#!/bin/bash -x
for F in *.*
do    mv $F "${F%.*}-00$(grep ${F%.*} episodes | cut -f3).${F#*.}"
done
$ ./script 
+ for F in '*.*'
++ grep bw032210 episodes
++ cut -f3
+ mv bw032210.mp4 bw032210-00.mp4
+ for F in '*.*'
++ cut -f3
++ grep bw042810 episodes
+ mv bw042810.mp4 bw042810-00.mp4
$ ls
bw032210-00.mp4    bw042810-00.mp4    episodes    episodes-bak    script
$

# 13  
Old 04-29-2010
your filenames are like bw######.??? !
grep tries to find bw032210 in episodes and there's none.
try
Code:
#!/bin/bash
for F in *.*
do
    N=${F%.*}
    mv $F "$N-00$(grep ${N#bw} episodes | cut -f3).${F#*.}"
done

# 14  
Old 04-29-2010
Quote:
Originally Posted by frans
your filenames are like bw######.??? !
grep tries to find bw032210 in episodes and there's none.
try
Code:
#!/bin/bash
for F in *.*
do
    N=${F%.*}
    mv $F "$N-00$(grep ${N#bw} episodes | cut -f3).${F#*.}"
done

Thank you! I figured I'd missed something, but even having re-read everything here I somehow missed that.

In your script I see where/how the bw fits, and how I could change it for anything else should the need ever arise.

I know it's purely educational now, as that does what I want, but on this script:
Code:
ls -tr *.* | gawk 'FNR==NR{a[$2]=$3;next}{printf("mv %s %s-%06d.%s\n",$0,$1,a[$1],$2)}' episodes FS="." - | sh

Where/how could I change it?

Thanks again.
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