Help Dynamic looping based on files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Dynamic looping based on files
# 1  
Old 11-09-2011
Question Help Dynamic looping based on files

Hi I have to run the script (a part of the code) in a loop for the no of times the files present in the directory, by taking one file and process and next another file.

For example, if we do ls and the result have:

Code:
$ls
abc.dat   def.dat   ghi.dat

The script code should loop for 3 times by taking abc.dat and process and next def.dat and then ghi.dat for processing.

Like this if we have 10 files, it should loop 10 times.

PS: Here the file names will change time to time, but the extensions are same (.dat).

Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-09-2011 at 11:06 AM.. Reason: code tags, see PM
# 2  
Old 11-09-2011
Have you tried this:
Code:
ls -1 *.dat | \
while read fname
do
     echo "Current file is: [${fname}]."
done
# Or you can use "find"
find . -name "*.dat" -type f | \
while read fname
do
     echo "Current file is: [${fname}]."
done

This User Gave Thanks to felipe.vinturin For This Post:
# 3  
Old 11-09-2011
try this
Code:
for filename in `find .  -type f -name "*.dat" -exec ls -1 {} \; | sed 's/\.\///'`
do
echo $filename
done

# 4  
Old 11-09-2011
That's a dangerous use of backticks and wholly unnecessary.

Code:
find .  -type f -name "*.dat" -exec ls -1 {} \; | sed 's/\.\///' | while read LINE
do
        echo "$LINE"
done

# 5  
Old 11-09-2011
If you have GNU "find" and you only need the filename (without path), you can use:
Code:
find . -name "*.dat" -type f -printf "%f\n" | \
while read fname
do
     echo "Current file is: [${fname}]."
done

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping in Perl based on defined keys in Map

Hello All, I am writing the below script where it will connect to database and returns the results. #!/sw/gcm/perl510/bin/perl use SybaseC; &openConnection; &loadvalues; sub openConnection { $dbproc = new SybaseC(SYDB}, $ENV{DBDFLTUSR}, $ENV{DBDFLTPWD}); if... (2 Replies)
Discussion started by: filter
2 Replies

2. Shell Programming and Scripting

Looping inside directories based on a file which contains file directory list

Hi All, Please help. I have got a file which contains a listing of a file and some directories after it, one by one. I am calling this file xyz.txt here file1 dir1 dir2 dir3 dir4 file2 dir5 dir6 dir7 dir8 file3 dir9 dir10 dir11 dir12 (6 Replies)
Discussion started by: Piyush Jakra
6 Replies

3. Shell Programming and Scripting

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (7 Replies)
Discussion started by: slatoms
7 Replies

4. Red Hat

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (1 Reply)
Discussion started by: slatoms
1 Replies

5. Shell Programming and Scripting

Dynamic SQL query based on shell script parameters

Hi, I need a script that will run a dynamic Oracle SQL. Dynamic meaning the SQL statement depends on the parameter. For instance, something like this: #!/bin/ksh -x # Set environment . /home/mine/set_vars sqlplus $LOGINID <<! >> /home/mine/log.txt select count(1) from $1 where... (2 Replies)
Discussion started by: laiko
2 Replies

6. Shell Programming and Scripting

Dynamic naming based on file content

I was just thinking if there is a way where i can dynamically rename files based on the actual file content. I have a load of pdf's which have been named wrongly. (We normally put date first, then brief description, then title) So can a script be written wherin, it pulls out the date and title... (2 Replies)
Discussion started by: deaddevil
2 Replies

7. Shell Programming and Scripting

Dynamic Variable Based on Count

I'm trying to assign variables that include the current value of a count, but I can't seem to get it working... this script is incomplete, but some guidance on how to use a dynamic variable would be helpful: Sample Input: bash-2.03$ more sg2.txt Results for group6 443 1394 Results for... (3 Replies)
Discussion started by: earnstaf
3 Replies

8. Shell Programming and Scripting

Looping through files...

I posted this in the Solaris forum, but I don't think it's platform specific, so I'm posting it here. Here is the situation. We are a company that has been using a professional publishing system, the software is called "ProType". It runs on Solaris 2.4, however it is no longer supported and we... (6 Replies)
Discussion started by: Fred Goldman
6 Replies
Login or Register to Ask a Question