[Solved] For loop help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] For loop help
# 1  
Old 02-27-2014
[Solved] For loop help

Hello,

This is really breaking my head. I request you help to solve this problem.


I have a list of files at the source directory (/tmp) as below,

NewTransfer_20131202_APAC.dat
NewTransferFile_20131202_APAC.dat
NewTransfer_20131203_APAC.dat
NewTransferFile_20131203_APAC.dat
NewTransfer_20131204_APAC.dat
NewTransferFile_20131204_APAC.dat

and some other files with dates which are not of my interest at the moment (/tmp).

NewTransfer_20131102_APAC.dat
NewTransferFile_20131102_APAC.dat
NewTransfer_20131103_APAC.dat
NewTransferFile_20131103_APAC.dat
NewTransfer_20131104_APAC.dat
NewTransferFile_20131104_APAC.dat



I want my desired output to be as below, coz im just trying to get the files for the date im passing through the first FOR loop (20131202, 20131203 & 20131204)

NewTransfer_20131202_APAC.dat
NewTransferFile_20131202_APAC.dat
NewTransfer_20131203_APAC.dat
NewTransferFile_20131203_APAC.dat
NewTransfer_20131204_APAC.dat
NewTransferFile_20131204_APAC.dat


but I get the following output,

NewTransfer_20131102_APAC.dat
NewTransfer_20131103_APAC.dat
NewTransfer_20131104_APAC.dat
NewTransfer_20131202_APAC.dat
NewTransfer_20131203_APAC.dat
NewTransfer_20131204_APAC.dat
NewTransferFile_20131102_APAC.dat
NewTransferFile_20131103_APAC.dat
NewTransferFile_20131104_APAC.dat
NewTransferFile_20131202_APAC.dat
NewTransferFile_20131203_APAC.dat
NewTransferFile_20131204_APAC.dat
NewTransfer_20131102_APAC.dat
NewTransfer_20131103_APAC.dat
NewTransfer_20131104_APAC.dat
NewTransfer_20131202_APAC.dat
NewTransfer_20131203_APAC.dat
NewTransfer_20131204_APAC.dat
NewTransferFile_20131102_APAC.dat
NewTransferFile_20131103_APAC.dat
NewTransferFile_20131104_APAC.dat
NewTransferFile_20131202_APAC.dat
NewTransferFile_20131203_APAC.dat
NewTransferFile_20131204_APAC.dat
NewTransfer_20131102_APAC.dat
NewTransfer_20131103_APAC.dat
NewTransfer_20131104_APAC.dat
NewTransfer_20131202_APAC.dat
NewTransfer_20131203_APAC.dat
NewTransfer_20131204_APAC.dat
NewTransferFile_20131102_APAC.dat
NewTransferFile_20131103_APAC.dat
NewTransferFile_20131104_APAC.dat
NewTransferFile_20131202_APAC.dat
NewTransferFile_20131203_APAC.dat
NewTransferFile_20131204_APAC.dat

SCRIPT:

Code:
dt="20131202 20131203 20131204"

export EXEC_DATE_TIME=`date +%Y%m%d%H%M%S`

src="/tmp"
dst="/tmp/inbox"
dtn="20131231"

for dname in $dt
do
        for fname in `ls $src/*_$dname_* | grep "Transfer"`
        do
				fn=`basename ${fname}`
				echo $fn
        done
done

# 2  
Old 02-27-2014
Try this:

Quote:
ls -1 | egrep -e "(20131202|20131203|20131204)" | grep "Transfer"
# 3  
Old 02-27-2014
The inner loop should be
Code:
  for fname in $src/*Transfer*_${dname}_*
  do
    [ -f $fname ] || continue  
    fn=`basename $fname`
    echo $fn
  done


Last edited by MadeInGermany; 03-04-2014 at 01:24 PM.. Reason: removed an offending line-break
# 4  
Old 02-27-2014
@ahmedwaseem2000: Thank you bro, ur suggestion works good when i remove the outer loop. Therefore making the code more efficient Smilie


@MadeInGermany: Thank you bro, ur code works exactly what i was expecting for Smilie


Thanks all for your valuable suggestions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have. If I use the following: && echo "echo failed" $? returns 1 When I use if ; then echo "echo failed" ; fi $? returns 0 Does anyone know what's wrong with this? Using AIX 6.1 and KSH for NUM in 1 2 3 do ... (5 Replies)
Discussion started by: jfxdavies
5 Replies

2. Shell Programming and Scripting

[Solved] Problem with if-then-else loop

Hi, i have a problem with this script: for i in $(cat list_ip_switch) do if if ; then echo "found ip" else echo "not found ip" fi done cat list_ip_switch 10.155.249.171 10.155.249.172 (3 Replies)
Discussion started by: elilmal
3 Replies

3. Shell Programming and Scripting

[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays , The following code works for ctcol in 2 3 4 5 6; do set -A a$ctcol $(cut -d, -f $ctcol test_file) done how ever this does not work for ctcol in {2..6}; do set -A a$ctcol $(cut -d, -f $ctcol test_file)... (4 Replies)
Discussion started by: 100bees
4 Replies

4. Shell Programming and Scripting

[solved] Process ssh command in while loop

I have a script that reads a file containing a list of server names. It's suppose to loop through the list of names and execute a command on the remote server using ssh. It processes the ssh command for the first server in the list and then exits. Here's the code: #!/bin/bash ... (2 Replies)
Discussion started by: westmoreland
2 Replies

5. Shell Programming and Scripting

[solved] Question for using variables outside a while loop

I want to get newvar outside the while any ideas? while read myline; do var=${myline} newvar1=$(let "$var") done echo $newvar1 I found it its ok now Thank you! (0 Replies)
Discussion started by: sanantonio7777
0 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused: I have 100 files in one folder 1. want to read each of the line by line 2. calculate their number of the words between the first word and the last word of each line 3. create file for each file with number of words... (8 Replies)
Discussion started by: A-V
8 Replies

7. Shell Programming and Scripting

[Solved] Use of until loop for user confirmation

Below is my script that is using to rename the name of file .Here I am using two methods to pass the both arguments wih script name or run the script and give the input one by one.But my issue is I want to rename the name of the file if user select Y(y) then it should rename the file else select... (4 Replies)
Discussion started by: anuragpgtgerman
4 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Simple while loop does not exit

spath="/home/user/k/${1}" dpath="/home/user/seq/Nov17/${1}" cd $dpath ls -1 $spath > list c=1 while read list newlist=`echo $list | sed 's/.gz//' ` newnewlist=`echo $newlist | sed 's/.fastq//' ` do echo $c echo $list c=$(($c+1)) (6 Replies)
Discussion started by: analyst
6 Replies

9. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

10. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 Replies
Login or Register to Ask a Question