Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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 05:32 PM.. Reason: updating title
Sponsored Links
    #2  
Old 02-07-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts
Quote:
Originally Posted by cburgoyne View Post
# 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: http://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 07:29 PM..
Sponsored Links
    #3  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Complex renaming then moving files yaledocker Shell Programming and Scripting 1 12-11-2011 04:24 AM
Moving and renaming large ammount of files sg3 Shell Programming and Scripting 4 12-07-2011 04:19 AM
Need script for renaming and moving files one by one... nypreH Shell Programming and Scripting 7 06-16-2011 06:30 AM
Moving multiple files and renaming them on the fly daemongk Shell Programming and Scripting 1 06-08-2007 01:36 PM
moving and renaming multiple files rocinante Shell Programming and Scripting 1 06-07-2007 08:20 PM



All times are GMT -4. The time now is 04:38 AM.