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


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-08-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
Reading the dates from a file & moving the files from a directory

Hi All,

I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another.

ie, this is how it will be in the file,

SUB_DATE = 20120608,20120607,20120606,20120606

Now, i need to move the file (with filename like a.20120608,a.20120607 etc) from /path/source to /path/destination.

Can anybody shed some light on how to achieve this ?

Thanks Much
Freddie
Sponsored Links
    #2  
Old 06-08-2012
spacebar's Avatar
Registered User
 
Join Date: Oct 2009
Location: spaceBAR Central
Posts: 294
Thanks: 0
Thanked 58 Times in 58 Posts
This will do it for you:

Code:
for x in `sed 's/^SUB_DATE = //;s/,/\n/g' test.txt`
do
  mv /source_directory/a.$x /target_directory
done

# File test.txt has records like:
SUB_DATE = 20120608,20120607,20120606,20120606

The Following User Says Thank You to spacebar For This Useful Post:
dsfreddie (06-09-2012)
Sponsored Links
    #3  
Old 06-09-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
Thanks Spacebar for your reply.

Yes,your code seems to work for all the dates except the first date in the file. ie 20120608. Here is the error message I got,

Quote:
++ sed 's/^P_SUB_DATE = //;s/,/\n/g' /iis_dev_data3/wcc/cpmg/tmp/test.txt
+ for x in '`sed '\''s/^P_SUB_DATE = //;s/,/\n/g'\'' /iis_dev_data3/wcc/cpmg/tmp/test.txt`'
+ mv /iis_dev_data3/wcc/cpmg/ctl/filename.P_SUB_DATE=20120608.dat.gz //iis_dev_data3/wcc/cpmg/inbox
mv: cannot stat `/iis_dev_data3/wcc/cpmg/ctl/filename.P_SUB_DATE=20120608.dat.gz': No such file or directory
+ for x in '`sed '\''s/^P_SUB_DATE = //;s/,/\n/g'\'' /iis_dev_data3/wcc/cpmg/tmp/test.txt`'
+ mv /iis_dev_data3/wcc/cpmg/ctl/filename.20120607.dat.gz //iis_dev_data3/wcc/cpmg/inbox
Can you pls help.

Thanks
Freddie

---------- Post updated at 11:49 AM ---------- Previous update was at 11:42 AM ----------

Never Mind, I figured out the issue.

The Space between the Equals ( = ) was causing the problem.

Thanks for your help. Appreciate it.

Freddie
    #4  
Old 06-09-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,454
Thanks: 108
Thanked 498 Times in 477 Posts
It appears that the contents of the file aren't exactly as you posted in your initial post. Working with spacebar's original post this might help:


Code:
sed 's/.*= //;s/,/\n/g' test.txt | while read x
do
  mv "/source_directory/a.$x" /target_directory/
done

Also note the change to read from sed's stdout rather than using backtics. Using the results of a command as input to the for isn't good practice (Useless Use of Cat Award there is a section on useless backtics too). I also think it's good practice to add a slant (/) to the end of the directory name on a mv command; if the directory doesn't exist, then you'll get an error rather than all files being moved to the same file name resulting in the loss of all files except the last.

Last edited by agama; 06-09-2012 at 11:56 AM.. Reason: additional thoughts
Sponsored Links
    #5  
Old 06-09-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
Thanks Agama. I agree with you. (Made the changes you suggested).

Now, i have a different issue. In the test.txt file, I have one more variable named BUS_DATE (& it comes first in the file).

$cat test.txt

BUS_DATE=20120609
SUB_DATE=20120608,20120607,20120606,20120605

Looks like it is taking the BUS_DATE & then goes to the SUB_DATE.

Pls find the error msg below,

Quote:
+ for x in '`sed '\''s/^SUB_DATE=//;s/,/\n/g'\'' path/test.txt`'
+ mv /path/filename.BUS_DATE=20120314.dat.gz /destination/ mv: cannot stat `/source/filename.BUS_DATE=20120314.dat': No such file or directory
+ for x in '`sed '\''s/^SUB_DATE=//;s/,/\n/g'\'' path/test.txt`'
+ mv /source/filename.20120608.dat /destination/
I dont need to pass the BUS_DATE. Any thoughts.

Thanks much
Freddie
Sponsored Links
    #6  
Old 06-09-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,454
Thanks: 108
Thanked 498 Times in 477 Posts
Sorry -- wasn't thinking about the possibility of other matches.

Try something like this:

Code:
sed '/SUB_DATE/ !d; s/.*=//; s/,/\n/g' input-file | while read x
do
   echo "$x"
done

It should ignore any line that doesn't contain SUB_DATE and then break each element from the comma separated list into a newline separated list that will be properly read by the while. Was thinking multiple lines with my suggestion.
Sponsored Links
Closed Thread

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
Grepping file names, comparing them to a directory of files, and moving them into a new directory sHockz Shell Programming and Scripting 1 02-02-2012 01:53 AM
Moving files listed in a data file to a new directory using Perl renthead720 Shell Programming and Scripting 2 01-15-2010 12:30 PM
Question on reading dates in folders/files bbbngowc Shell Programming and Scripting 3 09-25-2009 06:06 AM
Creating date directory and moving files into that directory ravi030 Shell Programming and Scripting 3 12-05-2008 03:18 AM
reading files for specific dates siva_jm Shell Programming and Scripting 3 06-08-2004 04:19 PM



All times are GMT -4. The time now is 11:09 AM.