Manipulating Filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating Filenames
# 1  
Old 10-30-2011
Manipulating Filenames

Hi Folks,
I'm looking for some ideas on how to change some file names. I'm pretty sure I need to use sed or awk but they still escape me. The files I have are like:

VOD0615 NEW Blades R77307.pdf or
VOD0615_NEW_Blades_R77307.pdf

and what I want after processing is:

R77307 NEW Blades.pdf

Essentially taking the first code off and moving the last code to the front. The description in the middle can be any amount of characters. The codes are usually seperated by a space or an underscore. The first code is always "VOD" then 4 numbers. The Last code is a single alpha with 5 numbers.

Any help appreciated.
# 2  
Old 10-30-2011
Assuming you pipe the output of ls to this awk, it will generate the move commands to rename the files. It's bare bones and doesn't do any error checking, so validate the commands it generates before running them.

Code:
ls VOD[0-9]*.pdf | awk '
    NF > 1 {        # assume spaces
        of = $0;
        split( $NF, a, "." );
        n = split( $0, b, " " );
        b[n] = a[1];
        sep = " ";
    }

    NF == 1 {       # assume underbars
        of = $1;
        split( $1, a, "." );
        n = split( a[1], b, "_" );
        sep = "_";
    }

    {
        printf( "mv \"%s\"  \"%s", of, b[n] );
        for( i = 2; i < n; i++ )
            printf( "%s%s", sep, b[i] );
        printf( ".%s\"\n", a[2] );
    }

' #| ksh

Remove the comment to pipe directly into kshell to move, but validate things first.
This User Gave Thanks to agama For This Post:
# 3  
Old 10-31-2011
Quote:
Originally Posted by imonkey
Hi Folks,
I'm looking for some ideas on how to change some file names. I'm pretty sure I need to use sed or awk but they still escape me. The files I have are like:

VOD0615 NEW Blades R77307.pdf or
VOD0615_NEW_Blades_R77307.pdf

and what I want after processing is:

R77307 NEW Blades.pdf

Essentially taking the first code off and moving the last code to the front. The description in the middle can be any amount of characters. The codes are usually seperated by a space or an underscore. The first code is always "VOD" then 4 numbers. The Last code is a single alpha with 5 numbers.

Any help appreciated.
Code:
for i in VOD*.*pdf; do mv "$i" "$(echo "$i"|sed 's/[^_ ]*[_ ]*\([^_ ]*\)[_ ]*\([^_ ]*\)[_ ]*\([^_ ]*\)\(\..*\)/\3 \1 \2\4/')"; done

Keep in mind,if you have both "VOD0615 NEW Blades R77307.pdf" and "VOD0615_NEW_Blades_R77307.pdf" files in the same dir,this code try to overwrite to last one.

regards
ygemici
# 4  
Old 11-06-2011
Thanks heaps agama that has worked a treat. I can barley understand it but I'll work on it a bit more. Quick question, why does it need to be piped to the kshell?

Hi ygemici,
Thanks for your help. I tried this but it only works on files that have two words between the codes so running on files with more or less messes things up. Examples below:
VOD0001 Test M00000.pdf
VOD0001 Test three words M00003.pdf
VOD0001 Test two M00001.pdf
VOD0615 NEW Blades R77307.pdf

ends up with:
Test M00000.pdf
words M00003 three.pdf
M00001 Test two.pdf
R77307 NEW Blades.pdf


Cheers
# 5  
Old 11-06-2011
Quote:
Originally Posted by imonkey
Thanks heaps agama that has worked a treat. I can barley understand it but I'll work on it a bit more. Quick question, why does it need to be piped to the kshell?

Not sure if your question was literally why is must be piped to a shell or if you meant must it be kshell -- would bash work. So, here are both answers:

The awk is generating the move commands, but needs kshell to execute them. It could be piped to bash, I just prefer Kshell so that's the way I tested it.

I'll add some comments to the code and maybe that will help you understand it a bit better.

---------- Post updated at 21:57 ---------- Previous update was at 21:45 ----------

Some additional info:
Code:
ls VOD[0-9]*.pdf | awk '
    NF > 1 {        # input line will have more than one field if filename has spaces
        of = $0;                    # save the original filename
        split( $NF, a, "." );       # split the last part of filename into array a using dot as seperator
        n = split( $0, b, " " );    # easy way to get all of the fields into an array
                                    # we put the fields into an array so we can treat both cases identically later
        b[n] = a[1];                # replace last field xxxx.yyyy with just xxxx
        sep = " ";                  # seperator to use when building the move to file
    }

    NF == 1 {                       # if just one field, asssume a filename without spaces
        of = $1;                    # save the original name
        split( $1, a, "." );        # split the name (xxx_yyy_zzzz.eee) on the dot xxx_yyy_zzzz goes into a[1] eee into a[2]
        n = split( a[1], b, "_" );  # split the leading lead part into array b using _ as separator
        sep = "_";                  # seperator to use when building the move to file
    }

      {                               # this block executed for all files; assumes array b has the filename components and n is the size of b
        printf( "mv \"%s\"  \"%s", of, b[n] );      # print command (mv) original name and  the last component of the name
          for( i = 2; i < n; i++ )   # starting with second component in the name print up to, but not including the last component
            printf( "%s%s", sep, b[i] );
        printf( ".%s\"\n", a[2] );      # add the extension (.xxx) and a newline
    }
' #| ksh

# 6  
Old 11-07-2011
Hi again Agama,
Wow, thanks heaps for all the comments. I've been advised that there some extra variables for the file names so I'll use those comments to try and modify myself. I'll get back to you if I need a hand if that's OK.

Thanks heaps
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulating files

Not sure if the question posted in another forums can be moved by me.So posting the link here. https://www.unix.com/unix-advanced-expert-users/221425-shell-script-manipulate-files.html#post302795379 Need your help here. (1 Reply)
Discussion started by: vedanta
1 Replies

2. UNIX for Dummies Questions & Answers

Manipulating files

Hi Guys, I'm really new to Unix and Linux and other scripting languages but recently I hv been really enthusiatic about learning more to help out on my work. So I have a file with 3 columns. A sample of it looks like looks like this : K2_537841 AAATCAGCCGCAACATTTGC ... (7 Replies)
Discussion started by: pawannoel
7 Replies

3. Shell Programming and Scripting

need help on manipulating a file

Hi, I need a shell/command to achieve this task. I've a delimited unloaded file from oracle in a scrambled format as shown below with many blank lines in it, I'm just trying to tailor it in a format that would be compatible to view and load it to a IDS db. Here is the problem ... (1 Reply)
Discussion started by: divak
1 Replies

4. Shell Programming and Scripting

Manipulating a file

Hi everybody, I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList: <PARAMETER NAME="PCList" TYPE="LIST_STRUCTURE" MODEL="{,}" ... (1 Reply)
Discussion started by: sameucho
1 Replies

5. Solaris

Manipulating TOP

I need the command top to output as: Memory: 2048M real, 1499M free, 53M swap in use, 5423M swap free on just the memory line. Instead, I have compiled the new version of top that displays as so: Memory: 2048M phys mem, 1499M free mem, 5476 total swap, 5423M swap free I read... (2 Replies)
Discussion started by: adelsin
2 Replies

6. Emergency UNIX and Linux Support

Manipulating Data

Hi. I haven't had to write bash scripts in a long time and have a simple task to do, but need some help: Input: chrY:22627291-22651542 chrY:23045932-23070172 chrY:23684890-23696359 chrY:25318610-25330083 chrY:25451096-25462570 chr10:1054847-1061799 chr10:1058606-1080131... (7 Replies)
Discussion started by: awknerd
7 Replies

7. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

8. UNIX for Dummies Questions & Answers

string manipulating

I have a file like this: Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 6 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 2 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 5 With this command seira=`cut -f 2 -d ';' tes.txt` i take all the results (6,2,5 etc) and i store them in variable seira When i do... (9 Replies)
Discussion started by: psalas
9 Replies

9. Solaris

Manipulating File

Help...please. I have a log that contains Warning Authentication Failed: User GHDT88998HS doesn't exit: The User GHDT88998HS could not be found Mar 22, 2008 5:22:22AM com.hometel.ttm.auth.userlogin. about maybe a thousand entries failed user acct message How can I grab just the username... (2 Replies)
Discussion started by: rivendell500
2 Replies

10. UNIX for Dummies Questions & Answers

date manipulating

I'm using Kshell scripts cronjobs to logrolling my daily log files and I stamped the log file at the end of the day by date stamp i.e.: %d_%m_%y-LogFile. but the 22_01_2001-LogFile as example contains 21st Jan data and I want to stamp the log file with previous day. so I'm trying to subtract... (3 Replies)
Discussion started by: tamer
3 Replies
Login or Register to Ask a Question