Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

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
Help Using AWK Script

Hi all

I have been researching awk and in my script testing I cant get awk to work in a script. I have a file that I need to move and rename into the new directory. I am pretty new to shell scripting. So any links or pointers to what I am trying to accomplish would be highly appreciated!!!!

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.

I have been trying something like this to pull out the information but it doesn't seem to rename the file


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

mv $fileName $new_name

Sponsored Links
    #2  
Old 02-07-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,192
Thanks: 233
Thanked 1,707 Times in 1,635 Posts
There's probably more efficient ways than running two entire instances of awk to process one file name.

What's your system? What's your shell?
Sponsored Links
    #3  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not sure but I am finding out. I know its a Lunix box and #!/bin/sh is on top of other scripts that are working
    #4  
Old 02-07-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,192
Thanks: 233
Thanked 1,707 Times in 1,635 Posts
LUnix is an experimental operating system for the commodore 64. Did you perhaps mean Linux?

If so there are many linux distributions. Try 'uname -a' to find out which.

Or perhaps you mean UNIX? Or just meant that you really don't know. In any case, try 'uname -a' to find out which.
Sponsored Links
    #5  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
LOL yes Linux.
I did find out that the System is a CentOS 5 box... default shell is BASH.
Sponsored Links
    #6  
Old 02-08-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,192
Thanks: 233
Thanked 1,707 Times in 1,635 Posts
Then you can do this:

Code:
#!/bin/bash

IFS="_." read RECIP CLIENTID CAMPAIGNID PSIJOBCODE CPACID BATCHNUMBER EXTENSION <<<"${FILENAME}"

mkdir -p $CAMPAIGNID
echo mv "$FILENAME" "${CAMPAIGNID}/${CPACID}"

Remove the 'echo' once you're sure it does what you want.
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
Help with shell script - Calling a script through script Siddheshk Shell Programming and Scripting 0 04-18-2011 05:14 AM
create a shell script that calls another script and and an awk script magikminox Shell Programming and Scripting 0 06-26-2008 02:50 AM



All times are GMT -4. The time now is 03:41 AM.