Sponsored Content
Top Forums Shell Programming and Scripting Renaming Files Based on Contents Post 302387496 by frans on Saturday 16th of January 2010 09:51:23 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
Exporter::Renaming(3pm) 				User Contributed Perl Documentation				   Exporter::Renaming(3pm)

NAME
Exporter::Renaming - Allow renaming of symbols on import SYNOPSIS
# Enable renaming in Exporter use Exporter::Renaming; # Import File::Find::find as main::search use File::Find Renaming => [ find => search]; # Disable renaming no Exporter::Renaming ABSTRACT
Allow Renaming of symbols on Import DESCRIPTION
Overview This module adds the ability to rename symbols to the standard Exporter module. After "use Exporter::Renaming", you can import symbols from exporting modules not only under their original names, but also under names of your choosing. Here, symbol is used to mean anything that could be exported by a Module, that is, a Perl function or variable. Thus a symbol begins with an optional type character (one of "$", "@", "%", "&", and "*"), followed by a name (a Perl identifier, made up of alphanumerics and "_", starting with a non-digit). To trigger renaming behavior, the import list of a subsequent "use <module>" statement must begin with the keyword 'Renaming', followed by a list reference, the <renaming list|/Renaming List>, which describes the renaming imports (see below). After that, a normal import list may follow, which Exporter processes as usual. Renaming List The renaming list contains renaming pairs, which are pairs of symbols. The first part of a pair is the original symbol (as known to the exporting module) and the second one is the renamed symbol (as you want to use it after import). It is an error (fatal, as all "Renaming" or "Exporter" errors) if the renaming list has an odd number of elements, or if one of its symbols is invalid. If none of the symbols in a renaming pair contains a type character, an "&" is assumed. If only one has a type character, this type is assumed for the other one too. If both have type characters, it is an error if they don't agree. If the renamed symbol (the second part) of a renaming pair is undefined, the original symbol is imported unchanged, so you can include normal imports in a renaming list without retyping the name. It is an error for a symbol to appear more than once as the second part of a renaming pair, that is, to specify the same thing twice as the target of a renaming operation. It is allowed to import the same symbol multiple times with different targets. Maybe it even makes sense in some situations. Operation Exporter continues to behave normally for normal imports while renaming behavior is switched on. Only the presence of the keyword "Renaming", followed by an array reference in the first and second positions after a "use" statement triggers renaming. The renaming behavior of Exporter is thus compatible with its standard behavior. If renaming must be switched off for some reason, this can be done via "no Export::Renaming". If an import list contains both a renaming list and a sequence of normal import statements, the renaming is done first, as indicated by its position. No cross-check is done between the results of renaming and the normal imports, as if these resulted from two separate "use" statements. EXAMPLES
All examples assume that use Exporter::Renaming; has been called (and that "no Exporter::Renaming" hasn't). The most obvious application of "Exporter::Renaming" is to solve a name conflict. Suppose our module already defines a function "find", and we want to use the standard "File::Find" module. We could then rename "find" from "File::Find" to "search" in our own module: use File::Find Renaming => [ find => 'search' ]; Let's assume the "finddepth" function from File::Find doesn't cause a name conflict, and we want to import it under its original name as well. This does it in the renaming list: use File::Find Renaming => [ find => 'search', finddepth => undef, ]; ...as does this, but explicitly: use File::Find Renaming => [ find => 'search', finddepth => 'finddepth', ]; ...while this uses a regular import: use File::Find Renaming => [ find => 'search' ], 'finddepth'; Should you find it annoying that a pedantic module author has chosen to adorn all of the module's exports with a redundant prefix (these things happen), you could do this: use Mythical::Graphics::Module Renaming => [ gfxColor => '%color', # this imports a hash gfxPen => 'pen', gfxLine => 'line', # .... # etc ]; ...lower-casing the names as well. If you need to add clarifying prefixes that a sloppy module author has neglected to provide in the exports (these things happen), you go the other way around: use Legendary::Graphics::Module Renaming [ Color => '%gfxColor', Pen => 'gfxPen', Line => 'gfxLine', # ... # etc ]; ...also lower-casing the initial letters. If you are confronted with a standard module that uses a slightly non-standard naming convention (it happens), you can rectify the situation: use Data::Dumper Renaming => [ Dumper => 'dump' ]; Now you can say "print dump %some_hash" instead of "print Dumper ..."; CAVEATS
o As has been mentioned in section Operation, no cross-check is done between renaming exports and normal exports that go on in the same "use" statement. This means that a renaming import may later be overwritten by a normal import without a clear indication. This happens when one of the new names given in renaming coincides with one of the original ones imported through normal import. o "Exporter::Renaming" only affects modules that do standard exporting, that is, modules that inherit their "import" method from Exporter. Modules that use a different "import" method are unaffected and don't understand renaming lists. o Renaming doesn't affect the name c<caller> sees for a function. This should come as no surprise, since normal export doesn't affect this name either. It is always the (package-qualified) name the function was originally compiled with. BUGS
o The lack of a cross-check between renaming and normal imports is regrettable, but unlikely to be fixed unless Renaming is made part of Exporter. Except for the simplest cases, only Exporter can parse an export list. o Calls of "use Exporter::Renaming" and "no Exporter::Renaming" don't nest. Instead of switching unconditionally, "no Renaming" should only switch off the behavior if it was off in the corresponding call to "use Exporter::Renaming". A future release may address this. SEE ALSO
Exporter, Perl AUTHOR
Anno Siegel, <siegel@zrz.tu-berlin.de> ACKNOWLEDGEMENTS
Thanks to Avi Finkel (avi@finkel.org) and Simon Cozens (simon@simon-cozens.org) for a discussion of this project on IRC. While brief, their remarks helped me think about things the right way. COPYRIGHT AND LICENSE
Copyright 2003 by Anno Siegel This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2010-04-23 Exporter::Renaming(3pm)
All times are GMT -4. The time now is 01:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy