Computing.Net > Forums > Linux > Script for read files in a director

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script for read files in a director

Reply to Message Icon

Name: rajaykumar_78
Date: January 16, 2006 at 02:13:49 Pacific
OS: Linux3AS
CPU/Ram: 3.0 gb
Comment:

Hi,
I am running Cisco Netflow at linux box, Every 15 mints some NFC application is creating files at cisco folder. i.e file is like this "/opt/CSCOnfc/Data/LinuxNFC/2006_01_14/10.76.5.101/DetailCallRecord/10.76.5.101.0200"
. Here only 2006_01_14 directory will change and 10.76.5.101.0200 file name will change. If i have 400 routers it will generates 10.76.X.X directories also. After that file is generated i am executing one perl script to read that file. i.e "perl NetflowPP.pl -f /opt/CSCOnfc/Data/LinuxNFC/2006_01_14/10.76.5.101/DetailCallRecord/10.76.5.101.0200" to convert excel sheet.
so i wanted to write one shell script to read data in date directory, router directory and inside router directory router with time stamp file for perl script input file. Here 4 variables are there. pls help me to write a script or pls give me if somebody having already writen scripts.
regards
Ajay



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: January 16, 2006 at 10:46:56 Pacific
Reply:

Your description of what you want is confusing.

Are you executing the perl script 400 times, once for each of the routers?

Are you asking for a script that gets the path to each file and then iterates over that list executing the perl script for each file?

Instead of a separate shell script, it would be easy to have the perl script locate the files.


0

Response Number 2
Name: rajaykumar_78
Date: January 16, 2006 at 22:31:40 Pacific
Reply:

FishMonger,

Thanks for reply, Yes i am aksing one script that is shell or perl which will get the path for each file where my NFC is placed,.Ok we will write perl script only which will reads files from reqired directories, In my above post i explain i having one directory structure where NFC is creating or placing files with router ip address like 10.76.5.101 i.e /opt/CSCOnfc/Data/LinuxNFC/2006_01_14/10.76.5.101/DetailCallRecord/10.76.5.101.0200"
/opt/CSCOnfc/Data/LinuxNFC/2006_01_14/10.76.10.153/DetailCallRecord/10.76.10.153.0200"
/opt/CSCOnfc/Data/LinuxNFC/2006_01_14/10.76.15.141/DetailCallRecord/10.76.15.141.0200"
up to /opt/CSCOnfc/Data/LinuxNFC/ is static,
/10.76.15.141/DetailCallRecord/ is static (where i added router ip address to collect NFC info at NFC application)
2006_01_14 is dynamic (every day creates one directory under /opt/CSCOnfc/Data/LinuxNFC, if next day it will create 2006_01_15).
10.76.5.101.0200 is dynamic (every 15 mints, if creates then next file is 10.76.5.101.0215 ).

Now after getting the file path from script i will pass this script out to my NetflowPP.pl file. Say you created fish.pl which is points to my directory file opt/CSCOnfc/Data/LinuxNFC/2006_01_14/10.76.5.101/DetailCallRecord/10.76.5.101.0200,
then i will execute NetflowPP.pl -f fish.pl ( -f is my input file).

In unix i have seen this command a=/opt/CSCOnfc/Data/LinuxNFC
b=`date '+%Y'"_"'%m'"_"'%d'`
cd $a/$b
pwd
then output is
/opt/CSCOnfc/Data/LinuxNFC/2006_01_14
so i ask for shell script, but i will write on perl only.
regards
Ajay



0

Response Number 3
Name: FishMonger
Date: January 18, 2006 at 08:08:37 Pacific
Reply:

Since I don't have any of your files to preform the proper testing, this may or may not work, but give it a try.

This forum is a joke when posting code because it strips out all of the intendation.

#!/usr/bin/perl -w

use strict;
use POSIX qw(strftime);

my $today = strftime("%Y_%m_%d", localtime);
my $basedir = "/opt/CSCOnfc/Data/LinuxNFC/$today";
my @routers = grep{-d $_}<$basedir/*>;

foreach my $router (@routers) {
my $records = "$basedir/$router/DetailCallRecord";
my @files = <$records/$router.*>;
foreach my $file (@files) {
system("NetflowPP.pl -f $basedir/$router/DetailCallRecord/$file");
}
}


0

Response Number 4
Name: FishMonger
Date: January 18, 2006 at 08:24:53 Pacific
Reply:

Actually, that's not going to work as expected. I forgot that the @routers array will hold the full path not just the filnal dirname (router IP). We need to remove the references to $basedir in the foreach loop.

See if this modified version works.

foreach my $router (@routers) {
my @files = <$router/DetailCallRecord/$router.*>;
foreach my $file (@files) {
system("NetflowPP.pl -f $router/DetailCallRecord/$file");
}
}


0

Response Number 5
Name: FishMonger
Date: January 18, 2006 at 08:27:32 Pacific
Reply:

Oops, I think I did it again.

system("NetflowPP.pl -f $router/DetailCallRecord/$file");

should be
system("NetflowPP.pl -f $file");


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Script for read files in a director

Reading files in multiple directori www.computing.net/answers/linux/reading-files-in-multiple-directori/29846.html

perl scripts for filtering mail www.computing.net/answers/linux/perl-scripts-for-filtering-mail/13380.html

help writing script to read files n www.computing.net/answers/linux/help-writing-script-to-read-files-n/27648.html