Use the information from filename in a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use the information from filename in a for loop
# 1  
Old 07-12-2012
Data Use the information from filename in a for loop

hi
here is my script



Code:
set -vx
b=`cat /info_d05/visage/SrcFiles/Customer_Master/Log_Files/last_date.txt`
for name in /info_d05/visage/SrcFiles/Customer_Master/Input_Files/*
do
fname=`basename $name`
p=`$fname|cut -d"_" -f6|sed 's/\(.*\)....../\1/'`
if [ $p -gt $b ]
then
cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/$fname /info_d05/visage/SrcFiles/Customer_Master
fi
 
done




now there is a date in the file last_date.txt in the format yyyymmdd....
in the location input_files there are files of different dates before after the date given in the file....
now i want to copy the files from input_files folder whose date is more than the date given in the last_date file...

that means for example

Code:
last_date has the value = 20120710
in input files the file names are given as below-
AZ_CM_VSG_ADDR_V001_20120711022154_001.dat
AZ_CM_VSG_ADDR_V001_20120710022154_001.dat

Now i want to copy the 2nd file from input file to the location /info_d05/visage/SrcFiles/Customer_Master

Please let me know where am i going wrong.Or is there any other simple solution using awk or perl or nythin....

---------- Post updated at 04:48 PM ---------- Previous update was at 04:45 PM ----------

hey sorry i want to copy the first file not the 2nd

---------- Post updated 07-12-12 at 11:47 AM ---------- Previous update was 07-11-12 at 04:48 PM ----------

HELLO EVERYONE PLEASE HELP ME...
I AM STUCK IN THIS CODE...HELP ME OUT...

Last edited by methyl; 07-11-2012 at 08:27 AM.. Reason: please use code tags
# 2  
Old 07-12-2012
check, whether these three lines, show the correct cp command or not

Code:
 
date=$(cat /info_d05/visage/SrcFiles/Customer_Master/Log_Files/last_date.txt)
ls -1 /info_d05/visage/SrcFiles/Customer_Master/Input_Files/*.dat > /tmp/FILE_LIST.txt
awk -F_ -v date="$date" 'substr($6,1,8)>date{print "cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/"$0" /info_d05/visage/SrcFiles/Customer_Master/"}' /tmp/FILE_LIST.txt

if you are fine with the cp command, then include |sh ( in the end of awk command )
# 3  
Old 07-12-2012
it ain't working....i need only one of the 2 files to copy...that is the one with date 20120711...here the command is executing such that both the files are taken into consideration but none are getting copied...the details are given below


Code:
 
./Visage_CM_To_Check_delay_Files.sh
date=$(cat /info_d05/visage/SrcFiles/Customer_Master/Log_Files/last_date.txt)
+ + cat /info_d05/visage/SrcFiles/Customer_Master/Log_Files/last_date.txt
date=20120710
ls -1 /info_d05/visage/SrcFiles/Customer_Master/Input_Files/*.dat > /info_d05/visage/SrcFiles/Customer_Master/Log_Files/FILE_LIST.txt
+ ls -1 /info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120710022154_001.dat /info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120711022154_001.dat
+ 1> /info_d05/visage/SrcFiles/Customer_Master/Log_Files/FILE_LIST.txt
awk -F_ -v date="$date" 'substr($6,1,8)>date{print "cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/"$0" /info_d05/visage/SrcFiles/Customer_Master/"}' /info_d05/visage/SrcFiles/Customer_Master/Log_Files/FILE_LIST.txt
+ awk -F_ -v date=20120710 substr($6,1,8)>date{print "cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/"$0" /info_d05/visage/SrcFiles/Customer_Master/"} /info_d05/visage/SrcFiles/Customer_Master/Log_Files/FILE_LIST.txt
cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files//info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120710022154_001.dat /info_d05/visage/SrcFiles/Customer_Master/
cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files//info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120711022154_001.dat /info_d05/visage/SrcFiles/Customer_Master/


Last edited by djrulz123; 07-12-2012 at 04:58 AM..
# 4  
Old 07-12-2012
hi,

Try using the concept of epoch time

Code:
 
set -vx
b=`cat /info_d05/visage/SrcFiles/Customer_Master/Log_Files/last_date.txt`
#new line of code
btime=`date +"%s" -d "$b"` 

for name in /info_d05/visage/SrcFiles/Customer_Master/Input_Files/*
do
fname=`basename $name`
p=`$fname|cut -d"_" -f6|sed 's/\(.*\)....../\1/'`

#new line of code 
ptime=`date +"%s" -d "$p"` 
#changed $p to $ptime and $b to $btime
if [ $ptime -gt $btime ]
then
#added / at end else cp will copy your file with the name Customer_Master under /info_d05/visage/SrcFiles/
cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/$fname /info_d05/visage/SrcFiles/Customer_Master/
fi
 
done

let me know if it works
# 5  
Old 07-12-2012
after running the above code i got this....both the files got copied in the end..what i can understand is that the value of p is not determeined properly...but when used independently it works properly...but inside this loop it doesn't seem to work..

Code:
+ + basename /info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120710022154_001.dat
fname=AZ_CM_VSG_ADDR_V001_20120710022154_001.dat
+ + AZ_CM_VSG_ADDR_V001_20120710022154_001.dat
+ cut -d_ -f6
./Visage_CM_To_Check_delay_Files.sh[9]: AZ_CM_VSG_ADDR_V001_20120710022154_001.dat:  not found
+ sed s/\(.*\)....../\1/
p=
+ + date +%s -d
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
ptime=
+ [ -gt ]
+ cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120710022154_001.dat /info_d05/visage/SrcFiles/Customer_Master/
+ + basename /info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120711022154_001.dat
fname=AZ_CM_VSG_ADDR_V001_20120711022154_001.dat
+ + AZ_CM_VSG_ADDR_V001_20120711022154_001.dat
./Visage_CM_To_Check_delay_Files.sh[9]: AZ_CM_VSG_ADDR_V001_20120711022154_001.dat:  not found
+ cut -d_ -f6
+ sed s/\(.*\)....../\1/
p=
+ + date +%s -d
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
ptime=
+ [ -gt ]
+ cp /info_d05/visage/SrcFiles/Customer_Master/Input_Files/AZ_CM_VSG_ADDR_V001_20120711022154_001.dat /info_d05/visage/SrcFiles/Customer_Master/

# 6  
Old 07-12-2012
make one more change

Code:
 
p=`echo $fname|cut -d"_" -f6|sed 's/\(.*\)....../\1/'`

your script would work too.. you just need to use
Code:
echo $fname

sorry i didn't notice it at first

Last edited by sam05121988; 07-12-2012 at 06:18 AM..
# 7  
Old 07-12-2012
great its working fine now....dnt knw how i missed it..

thanks a lot..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Emergency UNIX and Linux Support

Waiting for wildcard filename to exists in while loop

Hi Experts, We are developing a script which will wait for the trigger file(with datetime in the trigger file name). But the problem is when I use 'while' loop to wait for the file, it waits for the filename with wilcard in it that is wait for 'Trigger*.done' file. :eek: Below is the script ... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

3. UNIX for Dummies Questions & Answers

Copying files with spaces in the filename in a for loop

Hi all, I've been tangoing with this one for a couple of days now and I'm still not making any progress. Basically I'm trying to match three numbers in a string from a text file with matching numbers in a jpeg, and then copying the results to another folder. Data looks like this: Model:... (4 Replies)
Discussion started by: faceonline
4 Replies

4. Shell Programming and Scripting

loop and extract matching filename

i am unable to exact matching filename in the loop. Filename.in file contains Customer_Product_ Information_Customer_Product_ sale_Product_ /home_dir contains files CUST_INFO_990090_1111.csv "1","Customer Product Detail","1000","salary" "1","Information Customer Product... (2 Replies)
Discussion started by: onesuri
2 Replies

5. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

6. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

7. Shell Programming and Scripting

Print filename inside loop

Im want to print filename inside loop .. the code im using :- Filename_1=abc_20090623_2.csv.lk Filename_2=def_20090623_2.csv.lk i want to extract filename till .csv eg Filename_1=abc_20090623_2 Filename_2=def_20090623_2 How can i do this inside the for loop ... (3 Replies)
Discussion started by: r_t_1601
3 Replies

8. Shell Programming and Scripting

filename in loop

i have a filename_1=file1.dat filename_2=file2.dat i want to pass the filename in a loop for((i=1;i<=2;i++) do awk{print $1} $filename_$i.dat done how should i pass the filename (2 Replies)
Discussion started by: r_t_1601
2 Replies

9. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

10. Shell Programming and Scripting

grep and awk showing filename in loop

I am attempting to grep a list of files for a string an and then only extract the 3rd and 4th field of from the line. That's easy. But I want to prefix the line with the filename that the information came from. for filename in `ls -1 *.txt' do grep search_text $filename | awk '{print $3"... (5 Replies)
Discussion started by: sjohns6
5 Replies
Login or Register to Ask a Question