Reading the dates from a file & moving the files from a directory

Thread Tools Search this Thread
# 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
# 2  
This will do it for you:
for x in `sed 's/^SUB_DATE = //;s/,/\n/g' test.txt`
  mv /source_directory/a.$x /target_directory

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

These 2 Users Gave Thanks to spacebar For This Post:
# 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,

++ 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.


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

Never Mind, I figured out the issue.Smilie

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

Thanks for your help. Appreciate it.

# 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:

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

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 01:56 PM.. Reason: additional thoughts
# 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


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

Pls find the error msg below,

+ 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
# 6  
Sorry -- wasn't thinking about the possibility of other matches.

Try something like this:
sed '/SUB_DATE/ !d; s/.*=//; s/,/\n/g' input-file | while read x
   echo "$x"

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.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #435
Difficulty: Medium
Angular and AngularJS are web application frameworks to use for developing multi-page web applications.
True or False?

10 More Discussions You Might Find Interesting

1. Red Hat

Moving files with specific dates

Hi, These are the list of files in one directory in the server : # ls -lrt total 10120 -rw-r--r-- 1 root root 4484 Jul 8 2011 install.log.syslog -rw-r--r-- 1 root root 51890 Jul 8 2011 install.log -rw------- 1 root root 3140 Jul 8 2011 anaconda-ks.cfg drwxr-xr-x 2 root root... (2 Replies)
Discussion started by: anaigini45
2 Replies

2. Shell Programming and Scripting

Moving files into dirs corresponding to dates

I am trying to find a way to move files into corresponding date files. i=0 while read line do array="$line" (( i++ )) done < <(ls) cd $(echo ${array}) echo ${array}} pwd #cd "$(array}" ] || mkdir 2015 cd "2015" ] || mkdir 02-February ] || mkdir 03-March ] || mkdir... (10 Replies)
Discussion started by: newbie2010
10 Replies

3. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

4. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

5. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

6. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

7. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

8. Shell Programming and Scripting

Moving files listed in a data file to a new directory using Perl

Hi, I have a data file that lists a number of files. I want to move the files named in that one to another directory. Here's what I have: #!/usr/bin/perl -w open(FILE, "<collision.txt"); my @lines=<FILE>; foreach my $lines (@lines) { system("mv $lines collisions/."); } close(FILE); ... (2 Replies)
Discussion started by: renthead720
2 Replies

9. Shell Programming and Scripting

Question on reading dates in folders/files

Hi All, I have some folder that are named "FOLDERYYYYMM". I'm trying to piece together a .sh script that will look for the folder with the date. How can I get shell to see the date? (3 Replies)
Discussion started by: bbbngowc
3 Replies

10. Shell Programming and Scripting

reading files for specific dates

assume files are in a directory /data $ ls -ltr Just displaying Data and file name: 01/01/2004 file_3434_typea.dat 01/01/2004 file_3423_typea.dat 01/01/2004 file_3436_typea.dat 01/01/2004 file_3434_typeb.dat 01/01/2004 file_3423_typeb.dat 01/01/2004 file_3436_typeb.dat ... (3 Replies)
Discussion started by: siva_jm
3 Replies

Featured Tech Videos