Perl script, how can I use this scipt in different folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script, how can I use this scipt in different folders
# 1  
Old 03-08-2010
Perl script, how can I use this scipt in different folders

Hello,
I am a new programmer and currently having some difficulties writing a script. Your help will be very appreciated. In general, I have the below script running in one folder and and can get results from "OUPUT" file in the same folder. My question is I have several hundreds folder which all contain file "OUTPUT". How can I modify my below script to extract the results from all the OUTPUT file?
Thanks all in advance

Code:
#!/usr/bin/perl




# open file
  open FILE, "OUTPUT";
  @file = <FILE>;
  close FILE;

 # scan file
  $stop = scalar(@file);
  for ($n_line = 0; $n_line <= $stop-1; $n_line++) {
  $line = $file[$n_line];

if ($line=~/trace\/3\s*(\d.\d+E\-\d+)/) {
$average_pressure=$1;
open result1, ">>../pressure.txt";
#print  "$average_pressure\n";
print result1 "$average_pressure\n";
close result1; 
}

if ($line=~/trace\/3\s*(\d.\d+E\+\d+)/) {
$latt_length=$1;
open result2, ">>../lattice_vector.txt";
#print  "$latt_length\n";
print result2 "$latt_length\n";
close result2; 
}

if ($line=~/Average cell vectors\s*r.m.s. fluctuations /) {
chomp($file[$n_line + 2]);
	@matrix_line1 = split(/ +/, $file[$n_line + 2]);
        chomp($file[$n_line + 3]);
        @matrix_line2 = split(/ +/, $file[$n_line + 3]);
        chomp($file[$n_line + 4]);
        @matrix_line3 = split(/ +/, $file[$n_line + 4]);
		open result2, ">>../lattice_vector_error.txt";
#print  "$matrix_line1[4]\n";
print result2 "$matrix_line1[4]\n";
close result2; 
}

if ($line=~/Average pressure tensor\s*r.m.s. fluctuations/) {
chomp($file[$n_line + 2]);
	@matrix_line1 = split(/ +/, $file[$n_line + 2]);
        chomp($file[$n_line + 3]);
        @matrix_line2 = split(/ +/, $file[$n_line + 3]);
        chomp($file[$n_line + 4]);
        @matrix_line3 = split(/ +/, $file[$n_line + 4]);
#print "@matrix_line1\n @matrix_line2\n @matrix_line3\n";
$pressure_error=($matrix_line1[4]+$matrix_line2[5]+$matrix_line3[6])/3;
#print "$pressure_error\n";
open result1, ">>../pressure_error.txt";
print result1 "$pressure_error\n";
close result1; 
}

}


Last edited by pludi; 03-08-2010 at 10:07 AM.. Reason: code tags, please...
# 2  
Old 03-08-2010
try:
per2find . -name OUTPUT

which will give you a perl script to find the files you want.
you can integrate that into your script.

if you populate @ARGV
e.g.
@ARGV = <*.txt>;

you can use the basic syntax like so to automagically open and read files:
Code:
@ARGV = get-list-of-files;
while (<>) {
  ....
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How many times did the scipt ran?

Hi, Can we able to identify how many times any script was ran per day/week/month/year wise (2 Replies)
Discussion started by: penqueen
2 Replies

2. Shell Programming and Scripting

Validating uppercase/lowercase of user input with perl compared to unix folders

Hi, I need to copy files from a source directory to a destination directory in unix. I'm using the file::copy for the actual copy. The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower... (6 Replies)
Discussion started by: Furou
6 Replies

3. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

4. UNIX for Advanced & Expert Users

sysbase scipt error

Hi All, when is run this manually it work's i am UNable to get the proper .sql so that i can use this in my input file. I need to do this way only bcz of requirment. Sample script isql -Sxxx -Usa -Pyyyy -D master -o "abc_script.sql" << EOF echo USE master >> "abc_script.sql"... (1 Reply)
Discussion started by: oracle_coorgi
1 Replies

5. Shell Programming and Scripting

help writing a scipt to search for errors.

Hi, I am a beginner unix user. I would appreciate any help you guys can provide me with. What I am looking to do is the following. I have a log file that gets generated every morning. Example: /home/me/folder/temp.log temp.log will say "Socket connected" if the connection was successful,... (4 Replies)
Discussion started by: Jeffenri
4 Replies

6. Shell Programming and Scripting

cant get a counter to work in bash scipt, this is calling expect script

I have looked high and low, tryed lots of diffrent things but cant get a simple counter to work right. what i need is to increase a count ever time it finishes the test, pass or fail. example TEST PASS 1, NEXT TEST PASS 2, I curently have set foo o while {$foo <=5} { incr foo puts... (1 Reply)
Discussion started by: melvin
1 Replies

7. Shell Programming and Scripting

trigger a script based on the run status of another scipt

Im a newbee.. I have a script which runs few times daily. I want to write another script which should pick up the latest log generated from the last run of the first job and trigger a thrid script if the first script runs successfuly. Please help... Cheers (1 Reply)
Discussion started by: Athena
1 Replies

8. UNIX for Dummies Questions & Answers

UNIX/MS-DOs scipt

Is any one who know a good tutorial for Unix bash script and Ms-Dos scipt?? if yes, if is possible to upload it or give me the link??? What is the difference betwwen uvix and ms-dos script?? (1 Reply)
Discussion started by: Tom2
1 Replies

9. UNIX for Dummies Questions & Answers

Finding out process id in a scipt

Hi, If in a shell script i write a command ls > bla & ls The output is redirected to bla and the next ls starts as first one is going on in background. I want to find the PID of the first command. Thanks in advance (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

10. Shell Programming and Scripting

Scipt to do login

I want a script which will run in the background. $ sh s.sh& 523 Then this script should provide input to the Login command. $ login login: i.e instead of the user typing in the username and password, the script running in the background should provide the username and password assuming... (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question