• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

String manipulation with wildcards

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.
0
slocoach
Asked:
slocoach
1 Solution
 
ozoCommented:
perl -e 'rename $_,substr($_,7,6).".txt" or warn "$_ $!" for <AA*.txt>'
0
 
slocoachAuthor Commented:
Thanks for the fast response.  Could you explain it a little bit please?  I'm very new to scripting and the linux environment.  
0
 
slocoachAuthor Commented:
I'll be calling this script from a directory the script doesn't reside in.  Does that change the command any?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
ozoCommented:
<AA*.txt> will look for the files in the direcory that you called the script from.
Is that what you want?
0
 
slocoachAuthor Commented:
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.
0
 
slocoachAuthor Commented:
awk '{print} END {print "New line here"}' YourInputFile
0
 
slocoachAuthor Commented:
ozo, please give me a good description of what your code does and why so I can award your points, thanks.
0
 
sunnycoderCommented:
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
0
 
ahoffmannCommented:
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
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now