Rename or Copy to a New File a Text File based in its Contents

Posted on 2012-09-11
Last Modified: 2012-09-26
I have hundreds of text files with no-sense names like “UINI1234.TXT”, “UINI12345.TXT”, “UINI106__L01_001”, etc,
Inside these small text files, with sizes no more than 30 kb, typically 7 kb, are important data I need to be part of the text file name.
In these text files, the data I want to be part of the file name, always appear in the same position, as shown in the Power Point slide included.

Each Text File has three main blocks:

            SE FACTURO

The problem to be solved is:
1)      To read these data from each text file
2)      To rename the file (or copy to a new text file) with a  name structured as:
3)      To list in a new file the text files renamed or copied.

I include two files already renamed as an example.


It is possible that two different files have identical contents; in that case, we have to delete the later file. So, it is important to verify the file being processed would have a name already processed.

The solution has to work in windows 7, or in Unix-Linux, or in Excel VBA.

Thank you very much.
Question by:doublemoon

    Author Comment

    I am waiting for a response, I would appreciate your help.
    LVL 51

    Expert Comment

    as you did not specify where to find DATA1, DATA2, etc. here's just an example

    mv -i your-file `awk '/DIVISION CENTRO OCC/{a=$1;}/Numero De Cuenta/{b=$NF}END{print a"-"b".txt"}' your-file|sed -e 's/[:\/]/-/g'`

    Author Comment

    Thank you ahoffmann.
    That's the idea. I have some clarifications:
       I run the shell script in a Linux server and obtained a file named:


    The "?" character caused a little problem working with the renamed file, but I renamed it with:

    mv 2012* otherfile

    The DATA1,DATA2,... are described in the attached Power Point slide, and they are data contained in the original text files.

    The name of all the files I need to rename begins with "UINI"

    As example, the attached file UINI001.txt should be renamed as:


    That is the idea, I appreciate your valuable help.
    LVL 51

    Expert Comment

    mv -i UINI001.txt `awk '/DIVISION CENTRO OCC/{a=$1;}/Numero De Cuenta/{b=$NF}END{print a"-"b".txt"}' UINI001.txt|sed -e 's/[?:\/]/-/g'`

    If you have more such characters like ? it might be better to improve the final sed, i.e.

        sed -e 's/[^a-zA-Z0-9,._]/-/g'

    Author Comment

    Ok, ahoffmann.

    I obtained:

    There are no more "?" characters.

     ¿How do I cut so that it obtains only "DF25E" (3rd to 6th characters) for the name of the renamed file, and not the entire string "31DF25E034970100"?

    Thank you.
    LVL 51

    Expert Comment

    mv -i UINI001.txt `awk '/DIVISION CENTRO OCC/{a=$1;}/Numero De Cuenta/{b=substr($NF,3,5}END{print a"-"b".txt"}' UINI001.txt|sed -e 's/[?:\/]/-/g'`

    > only "DF25E" (3rd to 6th characters)
    is an ambigius definition, I used 5 characters ;-)

    Author Comment

    Thanks again, ahoffman.

    ¿How do I differentiate between the first and the second appearance in the file of the string "Numero De Cuenta"?

    I need to specify the first or second time it appears because the associated data may be different.

    Thank you for your help.
    LVL 51

    Accepted Solution

    mv -i UINI001.txt `awk '/DIVISION CENTRO OCC/{a=$1;}/Numero De Cuenta/{c++if(c==1){b=substr($NF,3,5}}END{print a"-"b".txt"}' UINI001.txt|sed -e 's/[?:\/]/-/g'`

    Author Closing Comment

    Thank you for your valuable help!
    LVL 51

    Expert Comment

    thanks for grading
    enjoy the magic of pipes and backticks ;-)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now