File Renaming and Moving


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Renaming and Moving
# 1  
Old 02-07-2012
File Renaming and Moving using AWK

Hello,

I need some help with a script. I have a file that I need to move and rename into the new directory. I am pretty new to shell scripting and have been trying to us awk with out success.

The file I will be getting is based in this format “Recipient_<ClientId>_<CampaignId>_<PSIJobCode>_<CPACId>.<BatchNumber>.pdf”

I need to pull out the <CampaignId> and the <CPACId> of the file to rename the file to <CPACId> and put the file into a newly created directory with the <CampaignId> as the name.

Currently I have been trying to use the following code
Code:
#!/bin/sh

# lood in common functions
scriptPath=$(dirname $0)
. $scriptPath/common.sh.lib

# requires 2 arguments
if [ $# -ne 2 ]; then
    echo "Usage: `basename $0` full_file_path external_path"
    exit 1
else
    # get filename without extension
    fileName=$(getFileName $1 'yes')
    
    new_name=`awk -F"_" '{print $5}' $fileName`
    CampaignId=`awk -F"_" '{print $3}' $fileName
    
    # move file
    createDir "//Web1/Proofs/$CampaignId"
    mv $fileName $new_name
    
fi

exit 0

The common.sh.lib was created by a developer who no longer works at my company but the following code should include all the calls that work in other scripts that we are using

Code:
# Get filename function
# arg1 $1 [required] - full path of file
# arg2 $2 [optional] - yes, strip off the file extension
getFileName() {
    if [ $# -lt 1 ]; then
        echo "Need to provide the full file path..."
        exit 1
    else
        if [ "$2" = "yes" ]; then
            # get filename without extension
            basename $1 | sed 's/\(.*\)\..*/\1/'
        else
            # get filename
            basename $1
        fi
    fi
}

# Get file path function
# arg1 $1 [required] - full path of file
getFilePath() {
    if [ $# -ne 1 ]; then
        echo "Need to provide the full file path..."
        exit 1
    else
        # get path only
        dirname $1
    fi
}

# Create directory function
# arg1 $1 [required] - path to directory
createDir() {
    if [ $# -ne 1 ]; then
        echo "Need to provide path to directory..."
        exit 1
    else
        # check if directory exist, if not create it
        if [ ! -d "$1" ]; then
            # make directory
            mkdir -p $1
        fi
    fi
}

# Check if file exist
# arg1 $1 = full path to file to test
fileExist() {
    if [ ! -f $1 ]; then
        echo "File does not exist..."
        exit 1
    fi
}

Any help would be much appreciated !!

Last edited by cburgoyne; 02-07-2012 at 06:32 PM.. Reason: updating title
# 2  
Old 02-07-2012
Quote:
Originally Posted by cburgoyne
# get filename without extension
fileName=$(getFileName $1 'yes')

new_name=`awk -F"_" '{print $5}' $fileName`
CampaignId=`awk -F"_" '{print $3}' $fileName


# move file
createDir "//Web1/Proofs/$CampaignId"
1. If you strip filename off its extension by providing "yes" as a parameter to getFileName, then which file are the next awk statements processing? For e.g., Suppose $1 contains "/home/user/test.dat". If you were to do this: fileName=$(getFileName $1 'yes'), $fileName would contain simply "test" and not "test.dat". Do you want your awk statements to process "test" or "test.dat"?

2. Second awk statement has a missing closing backtick.

3. In your main script, there's a condition to check if [ $# -ne 2 ], but I don't see $2 being used anywhere.

4. Also, in createDir "//Web1/Proofs/$CampaignId", the two forward slashes (//Web1) appears to be a typo-error. First slash not required.

---------- Post updated at 06:04 ---------- Previous update was at 05:53 ----------

I just saw another related post: https://www.unix.com/unix-dummies-que...wk-script.html

Try using the getFileName function without second parameter - 'yes'. Anyway, its optional from the way its coded in common.sh.lib

Last edited by balajesuri; 02-07-2012 at 08:29 PM..
# 3  
Old 02-08-2012
Ya I wasn't getting any responses on this forum so I got a little worried that I posted this in the wrong section. I will try your suggestions and see what happens. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for renaming and moving Files - Easy?

Hey guys, ive been working on this for about 2hrs now - without any solution. At first I need to say I dont have skills in linux bash scripting, but I tried to use some codesnippets and manuals from google. What I want to do: I have different folders including 2 different filestypes with... (15 Replies)
Discussion started by: peter1337
15 Replies

2. UNIX for Dummies Questions & Answers

Moving and renaming files

I have a directory full of directories, say called A B C D E .... In each of these directories there are files called 1.dsp 2.dsp 3.dsp ..... along with others (with different extensions) I need to go through each of these directories and move the dsp file to another folder, but with the name now... (6 Replies)
Discussion started by: claire.a
6 Replies

3. Shell Programming and Scripting

Moving and renaming multiple files in a directory

Hi. I am trying to automate the movement and renaming of a number of files in a directory. I am using the 'mv' command as I do not have access to 'rename'. I have the following scripted FILES=$(ls /transfer/move/sys/mail/20130123/) if ; then for i in ${FILES} ; do mv... (4 Replies)
Discussion started by: jimbojames
4 Replies

4. Shell Programming and Scripting

Complex renaming then moving files

I am a biologist and using an program on a computer cluster that generates a lot of data. The program creates a directory named ExperimentX (where X is a number) that contains files "out.pdb" and "log.txt". I would like to create a script that renames the out.pdb file to out_ExperimentX.pdb (or... (1 Reply)
Discussion started by: yaledocker
1 Replies

5. Shell Programming and Scripting

Moving and renaming large ammount of files

Hey, I'm kinda new to the shell scripting and I don't wanna mess things up yet :) Looking for a solution to the following: I need to move all the files like "filename.failed.dateandtime" to another directory also renaming them "filename.ready". I can't figure how to do this with multiple files... (4 Replies)
Discussion started by: sg3
4 Replies

6. Shell Programming and Scripting

Need script for renaming and moving files one by one...

Dears, I need your help! I got a problem and found some workaround solution but I donno how to realize it. I have a number of files (about 300 each day) and I need them to be renamed. All these files has fixed number of letters and name looks like this one:... (7 Replies)
Discussion started by: nypreH
7 Replies

7. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

8. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

9. Shell Programming and Scripting

Moving multiple files and renaming them on the fly

Hi All, Being new to scripting I am facing a new situation. We have an application that generates a file lets say dumpfile for each user under the users home directory when they execute the application. This is quite a huge file and imagine having that for over 40 users on a daily basis. The... (1 Reply)
Discussion started by: daemongk
1 Replies

10. Shell Programming and Scripting

moving and renaming multiple files

Greetings, I know i can use the mv command to move and rename one file. How can I do this with multiple files? example pic01.bmp to pic0001.bmp how can i perform this function on an entire directory of sequential files and keep them in sequence? Hints, suggestions are most welcome:) ... (1 Reply)
Discussion started by: rocinante
1 Replies
Login or Register to Ask a Question