String manipulation with wildcards

Posted on 2006-05-05
Last Modified: 2010-04-22
I'm writing a processing script and need help.  

I want the script to go through a directory populated with files that are similar in format to this: AA-BBB-20060505555-xxxxxxxxxxxxx.txt
and I want to pull off the first 7 characters, keep the next 6, blow away the rest and stick an extension on it.

So the end result for the example file would be 200605.txt

The part that is giving me the most trouble is the grabbing and renaming of files.  I can't figure out how to 1) grab each file in the directory and 2)parse out what I want from it's name.  I think I have a good handle on how to add extensions and the rest of what I need to do.

Fyi, the files in the folders are not static, they will always start with AA and end with .txt, but that's where the similarites stop.
Question by:slocoach
    LVL 84

    Accepted Solution

    perl -e 'rename $_,substr($_,7,6).".txt" or warn "$_ $!" for <AA*.txt>'

    Author Comment

    Thanks for the fast response.  Could you explain it a little bit please?  I'm very new to scripting and the linux environment.  

    Author Comment

    I'll be calling this script from a directory the script doesn't reside in.  Does that change the command any?
    LVL 84

    Expert Comment

    <AA*.txt> will look for the files in the direcory that you called the script from.
    Is that what you want?

    Author Comment

    No, i'll give you the whole picture because i'm a newb and have a hard time conveying what i'm trying to do.

    Here's the scenario.
    The script will reside in /usr/local/bin
    I have a folder I need to look at /var/logs/archive.  I am going to awk stuff out of each file in that folder and append that data to one file.

    Now that I think about it, I don't need to rename anything.  I just need to awk stuff out of each dynamically named file that starts with AA and append it to a file.
    I probably need to restart the question eh?  

    If you can answer this latest query that's great and i'll give you the points, otherwise, i'll give you the points anyways and open another question.

    Author Comment

    awk '{print} END {print "New line here"}' YourInputFile

    Author Comment

    ozo, please give me a good description of what your code does and why so I can award your points, thanks.
    LVL 45

    Expert Comment

    Give the points to ozo .... Here is explanation of his code

    > perl -e 'rename $_,substr($_,7,6).".txt" or warn "$_ $!" for <AA*.txt>'
                  ^^ Renames
                             ^^Name currently in buffer ( this would be the name of file for each loop iteration) ($_)
                                     ^^^ New name would be the substring of length 6 from 7th character onwards of the current name (in buffer)
                                                        ^^^Append .txt to the name
                                                                        ^^ Raise a warning if cant rename
                                                                                              ^^^ Loop for all files which match pattern AA*.txt
    LVL 51

    Expert Comment

    slocoach, ozo gave a bullet proof answer, please open a new question for more questions

    > Now that I think about it, I don't need to rename anything.  I just need to awk stuff out of each dynamically named file that starts with AA and append it to a file.
    ls AA* >>file

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    731 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

    19 Experts available now in Live!

    Get 1:1 Help Now