Looping

Hi,

Now I have written a script which sorts the records in the file and splits them according to some condition.

Now, I need to modify the script so that it reads all the files one after the other and does the sorting & splitting.

Pls help me in reading all the files in a directory and using the above logic which I have already.

Regards,
Sunitha

You can again use for loop and read files one by one. like

cd /source/dir
for file in *.ext
do
    ----
    ----
done

If you can explain with example, it would be more easy to understand us.

  • nilesh

Something like this?

for file in *
do
  #do your stuff here with $file
done

If this is not what you're looking for, post your script, and we'll see how we can assist.

Regards

My script is:

cd /app/chdata/workflow/suppl/esoutput/spd/flatfile
sort testfile1.txt | awk '{ file=substr($0,1,2)".txt"; print >> file }'

This script reads particular file named testfile1.txt and sorts the records and splits them as per some condition.

Now, instead of hardcoding one file say testfile1.txt, I want to modify this script where it reads all the files in the directory /app/chdata/workflow/suppl/esoutput/spd/flatfile and does the above logic.

Can anyone modify my script so that my requirement is fulfilled?

Yeah, this would for instance simply output every line of Perl scripts in the current dir.
Instead of simply echo you can do any sort of processing in the inner while loop.
e.g.

for f in *pl; do
    echo "Content of $f:"
    while read line; do
        echo $line
    done < $f
done

My script is:

#!/usr/bin/ksh
cd /app/chdata/workflow/suppl/esoutput/spd/flatfile
sort testfile1.txt | awk '{ file=substr($0,1,2)".txt"; print >> file }'

This script reads particular file named testfile1.txt and sorts the records and splits them as per some condition.

Now, instead of hardcoding one file say testfile1.txt, I want to modify this script where it reads all the files in the directory /app/chdata/workflow/suppl/esoutput/spd/flatfile and does the above logic.

Can anyone modify my script so that my requirement is fulfilled?

Its working
Thanks!!!:b:

You've already been given examples with the 'for' loops. Where exactly are you stuck?

I got it resolved

Thanks!!!