Renaming Files Based on Contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming Files Based on Contents
# 1  
Old 01-16-2010
Renaming Files Based on Contents

Hello everyone,

I currently have a situation which is causing me some issues for keeping up with certain files. I will explain this to the best of my abilities.

I have a list of files as follows

50_REPORT_1111 - file contains the word Car
50_REPORT_2222 - file contains the word House
50_REPORT_3333 - file contains the word Dog
50_REPORT_4444 - file contains the word Apple
50_REPORT_5555 - file contains the word Orange
50_REPORT_6666 - file contains the word Grape

As you can see the name between the _ and the _ are all the same, however each file contains different information. The files are all text.

So what i'd like to do is some sort of Unix script that would go through and read the files if they contained "_REPORT_" in the file name and after the first time it matched the word Car while reading the file, it would then rename the file to 50_REPORT.CAR_1111 and then continue searching the specified directory for another file that contains _REPORT_ until there are no more left and all of the files have been renamed.

I am honestly not sure where to start this, so if someone could at least point me in the right direction I might be able to figure it out from there.

I hope what I said makes sense.

Thank you for your time reading this.
# 2  
Old 01-16-2010
You should have the list of words you're searching for.
Code:
for F in $(ls *REPORT_*)
do
    for W in $LIST_OF_WORDS
    do
        if grep $W $F
        then
            NEW=${F:0:9}.$W_${F:9}
            echo $NEW
            # mv $F $NEW # Uncomment this to process
            break
        fi
    done
done

# 3  
Old 01-16-2010
I understand the idea... I think.

You are making the value of F the file name by listing any file in the directory with *REPORT_*

For W in you are saying have a seperate file with all of the words in it. I see you are using $LIST_OF_WORDS as the variable but where do you call in the actual file name?

Also if the word or phrase that is the file is CARS WITH RED PAINT, I may rename that file to 50_REPORT.CARSRED_1111, so based on what I understand from what you wrote it is using the list of words to write out the file name. So if I had CARS WITH RED PAINT in the list of words or the phrase, it'd make the report name 50_REPORT.CARS WITH RED PAINT_1111. Is there a way to have it make the name of the files different?

I apologize I am trying to pick this up but what you wrote is a little bit over my head.
# 4  
Old 01-16-2010
You are understanding how it works.
One thing you can do is to make a file with the phrases and the corresponding name you want to insert in the filename like :
Code:
"CARS WITH RED PAINT"    CARSRED
"CARS WITH BLUE PAINT"    CARSBLU
...

The script would become
Code:
for F in $(ls *REPORT_*)
do
    while read W N
    do
        if grep "$W" $F
        then
            NEW=${F:0:9}.$N_${F:9}
            echo $NEW
            # mv $F $NEW # Uncomment this to process
            break
        fi
    done < file-with-names
done

# 5  
Old 01-17-2010
Ok so to test this, to make sure I am doing this right... I created the file "names" and i placed
Code:
"CARS WITH RED PAINT"    CARSRED
"CARS WITH BLUE PAINT"    CARSBLU

in the file.

I then created two files, one that had nothing but CARS WITH RED PAINT and one that had nothing but CARS WITH BLUE PAINT. Files were named 99_REPORT_2222 AND 99_REPORT_3333 and I placed them into the same directory. I then just changed the script you wrote out from "file-with-names" to "names and placed that into a file called "testscript". I did a chmod 777 so I could execute it no the testscript and then ran it. The result was
Code:
root@testbox:/testscript # ls
99_REPORT_2222  99_REPORT_3333  names           testscript

So I am assuming I am doing something wrong or not understanding exactly how I am suppose to do it.

Any advise would be appreciated.

Thank you

After reading online for a bit I found "while read line" instead of while read, I am not sure if it is applicable but I did get a little further, or so it seems. Instead of coming directly back to a # prompt it now shows..

Code:
root@testbox:/testscript # ./testscript
CARS WITH RED PAINT
./testscript[7]: NEW=${F:0:9}.$N_${F:9}: bad substitution
root@testbox:/testscript #

Side note, based on what I can tell it is not pulling the $N value which I am assuming is suppose to be the new name of the file

Last edited by pludi; 01-17-2010 at 11:04 AM.. Reason: code tags, please...
# 6  
Old 01-17-2010
Ok i found what went wrong :
I'ts i the read which retruns something like
Code:
$W="CARS
$N= WITH RED PAINT" CARSRED

You will have to put the name before the string and no need to quote it l
names:
Code:
CARSRED    CARS WITH RED PAINT
CARSBLU    CARS WITH BLUE PAINT

testscript:
Code:
#!/bin/bash
for F in $(ls *REPORT_*)
do
    while read N W
    do
        if grep -q "$W" $F
        then
            NEW=${F:0:9}.${N}${F:9}
            echo $NEW
            # mv $F $NEW # Uncomment this to process
            break
        fi
    done < names
done

# 7  
Old 01-17-2010
I made the modifications that you mentioned but I am still stuck on this error.

./testscript[7]: NEW=${F:0:9}.${N}${F:9}: bad substitution


Any ideas where this one might be coming from?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove a line based on contents of the first column?

Good day all. Using basic UNIX/Linux tools, how would you delete a line based on a character found in column 1? For example, if the CITY name contains an 'a' or 'A', delete the line: New York City; New York Los Angeles; California Chicago; Illinois Houston; Texas Philadelphia;... (3 Replies)
Discussion started by: BRH
3 Replies

2. Shell Programming and Scripting

Computing difference based on line contents

I have the following awk script set up to copy the contents of a line that contains 0008 in each line that contains values of 1895 through 2012. awk -v OFS=" " '{val=0+substr($1,length($1)-3,4);if(val==0008){print;$1=x;y=$0}else{if(val>=1895&&val<=2012){print $1 y}else{print}}}' Output... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

3. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

4. Shell Programming and Scripting

Concatenating many files based on a specific column contents

Dear all, I have many files(.csv) in a directory. I want to concatenate the files which have similar entries in a particular column and save into a new file like result_datetime.csv etc. One example file is like below. Sno,Step,Data1,Data2,Data3 etc. 1,0,2,3,4 2,1,3,4,5 3,2,0,1,1 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Splitting large file and renaming based on field

I am trying to update an older program on a small cluster. It uses individual files to send jobs to each node. However the newer database comes as one large file, containing over 10,000 records. I therefore need to split this file. It looks like this: HMMER3/b NAME 1-cysPrx_C ACC ... (2 Replies)
Discussion started by: fozrun
2 Replies

6. UNIX for Dummies Questions & Answers

count values based on contents of another file

Hello, I have two files as shown below: test1 678 679 689 690 710 test2 1 678 654 800 676 791 689 900 I want to get a count of lines from test2 whose columns bound the values in test1 I tried running the code below; however am getting wrong results. (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

7. Shell Programming and Scripting

Renaming files based on data in a spreadsheet

I have a spreadsheet in a folder that looks like this: Sector1...Sector2...Sector3...Sector4...Sector5...Sector6...Sector7 SomeID....Title.......SomeID...SomeID....SomeID...SomeID....SomeID OtherID...MyTitle....SomeID...SomeID....SomeID...SomeID....SomeID... (7 Replies)
Discussion started by: Xterra
7 Replies

8. Shell Programming and Scripting

Manipulating word based off of contents

Hello everyone, my first post here, please feel free to inform me if my question can be better formatted so my future posts may be more clear. I have a large text file which I need parsed in one specific way, I have done the rest of the processing, I am simply lacking the last aspect of such. ... (8 Replies)
Discussion started by: ryanfx
8 Replies

9. Shell Programming and Scripting

Merging files based on the contents

Hi, I have a file f1 having the contents as below select (<condn>) from dual I have another file f2 having the contents as below 1, 2, 3 I want to replace <condn> in f1 with the contents of f2 I tried using sed like this sed "s:<condn>:`cat f2`:g" f1 The above command resulted in sed:... (3 Replies)
Discussion started by: mr_manii
3 Replies

10. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies
Login or Register to Ask a Question