Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract some file n move the files

Status
Not open for further replies.

moretips

MIS
Sep 25, 2000
25
US
#!/usr/bin/perl

opendir (PU, ".") || die "serious dainbramage: $!";

open(OUTPUTFILE,">list.txt") || die "Failed to open OUTPUTFILE, $!\n";

#@greplist = grep { m/^[p]// } readdir PU

while (<PU>)

{

@line = grep {CONNECT},PU;

print OUTPUTFILE &quot;@line&quot;;

}

close OUTPUTFILE;

closedir PU;

-------------------------------------------------------
Hi, above is my &quot;failed&quot; scipt. i wish to write a scipt which has some function:

1) From an dir, read files which the name is p....., if the content of that p.... files has the word &quot;CONNECT&quot;, MOVE all that files in one file. For example, C file has the files name which their content has the word &quot;CONNECT&quot;.

2) I will call a script, xx (while (<>){
printf &quot;%s\n&quot;, substr($_,3,20); } )

which will extract the date of the file name in C file. The output shoud be in another file, D which has the name of that file n also the date.

3) Then i will mail this D file to somebody.

i think i know how to do with num 3. The hard thing is num 1 n 2.

Pls help me, thank a lot.
 
sorry ... one more quetions..

I have searched this forum for how to send attachement , but couldn't found.

May i know, after the operation 1 n 2 above, how to send that file, D to somebody.

thanks a lot..................................................................
 
i am wonder why no body answer my question ......

am my question too difficult or not clear ..

 
Personally, I've read your question and it piques my curiosity, but I don't have time to research what you need. You are basically asking for the code to a fairly large program. What you have given is not sufficient. If I have time in the future, I may post on the question of how to read directories or how to send an attachment. Sorry I cannot do so right now.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
First do all this with the standard unix command 'find' like this:
find topdir -exec grep &quot;CONNECT&quot; {} \; -exec ls {} \; -exec cat {} \;
then convert this to a perl script with find2perl which comes with perl.
 
moretips,

I agree with Tom, you're asking quite a lot for someone to write that script for you.

Can I suggest that you make an attempt at the script and then if you have a problem - you may not, post again with a specific query.

Mike
michael.j.lacey@ntlworld.com
 
Ok .... Below is my script:

#!/usr/bin/perl

opendir(DIR,&quot;.&quot;) || die &quot;Serious dainbramage: $!&quot; #go to current directory

#@allfiles = grep { CONNECT } readdir DIR;

#@allfiles= grep CONNECT, readdir DIR;

@allfiles = grep( /CONNECT/, readdir DIR); #grap the files which has the word

closedir DIR; #connect in the content

open (CF, &quot;>cf.txt&quot;);

foreach $allfiles (@allfiles) { #for each file i will extract the date

@filelist = substr($allfiles, 3, 20); # and print it in the cf.txt

print CF &quot;@filelist&quot;; # so , the cf.txt will have the p.. file

} # and date of the p*** file

close (CF);

open (MAIL, &quot;| /usr/sbin/sendmail -t &quot;); # then i will mail it to somebody

print MAIL qq~To: &quot;Kenix&quot; <kenix_neo@yahoo.com>
print MAIL qq~\nFrom: &quot;Kenix&quot; <kenix_neo@yahoo.com>
print MAIL qq~Subject: Testing\n~;

print MAIL qq~Content-Type: text/plain\n\n~;

print MAIL qq~@filelist\n~;

print MAIL qq~.~;

close(MAIL);

#unlink @allfiles; # remove the p* file in my

#current directory
---------------------------------------------------------------------------------------------------

hmmm ... above is my script and explaination. The result i get is a &quot;blank&quot; cf.txt.

It seem that i have to write a lot of perl script to do this job done, but in shell script ,
just few lines of script is enough (not included the mail function):

#/usr/bin/ksh

grep -l CONNECT p* > c

for i in `cat c`
do
xx $i >> /apps/xxx/abc/laser/work/back/dcon
mv $i /apps/xxx/abc/laser/work/back
done
~

------------------------------------------------------------------ xx in above is a script which is :

#!/usr/bin/perl

while (<>){
printf &quot;%s\n&quot;, substr($_,3,20);

}

------------------------------------------------------------

My query is why my script doesn't work the same way like the shell script ? Anything wrong with my script ? Can anybody help me ???

Thanks a lot .................

 
OK, something to work with.....;^)

First, you need a ';' at the end of the 'opendir' line.

Second, I don't think Perl's grep will work the way you are using it. While the UNIX grep operates on file names, Perl's grep does not. The Perl function needs a list of strings to operate on. The 'readdir' function is returning a list of file names, so, those file names are what the grep function is operating on. You need to catch the file names and then turn around and read the files....

If you are comfortable with the shell syntax, you could just put the shell verbage in backticks.

Or, if you want/need to convert all completely to Perl....

This is a little verbose, but it shows the flow for getting a list of files, checking each one, and keeping the ones that have 'CONNECT' in them.

#!perl
opendir(DIR,&quot;.&quot;) || die &quot;Serious dainbramage: $!&quot;;
@allfiles = grep !/^\./, readdir DIR; # get a list of files in DIR
closedir DIR;

# open an output file
open(OPF,&quot;>c.txt&quot;) or die &quot;Failed to open outputfile, $!\n&quot;;

foreach $file (@allfiles)
{
open(IPF,&quot;<$file&quot;) or die &quot;Failed to open IPF, $!\n&quot;;
while($line = <IPF>)
{
if ($line =~ /connect/i)
{ # catch substr from 0 to 6 of a line containing the string 'connect'
$date = substr($line,0,6); # adjust this to catch you date.
print OPF &quot; $file\t$date\n&quot;;
}
}
close IPF;
}


You appear to be making a similar mis-use of substr. You are applying the substr function to file names when I think you need to be applying it to the contents of each file. The above code illustrates opening/reading/closing files and using substr on the contents of them.

Again, you can embed your shell statements in your Perl if you like using backticks or pipes or exec or system ..... depending on what you want to do.

'hope this helps...


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top