|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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:
Thanks much Freddie |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|