Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Extract some file n move the files

Extract some file n move the files

Extract some file n move the files


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 "@line";                                               
close OUTPUTFILE;                                                       
closedir PU;                                                         

Hi, above is my "failed" 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 "CONNECT", MOVE all that files in one file.  For example, C file has the files name which their content has the word "CONNECT".

2) I will call a script, xx (while (<>){                       
  printf "%s\n", 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.

RE: Extract some file n move the files

anybody help .........

RE: Extract some file n move the files

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..................................................................

RE: Extract some file n move the files

i am wonder why no body answer my question ......

am my question too difficult or not clear ..

RE: Extract some file n move the files

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.

Tom Anderson
CEO, Order amid Chaos, Inc.

RE: Extract some file n move the files

First do all this with the standard unix command 'find' like this:
find topdir -exec grep "CONNECT" {} \; -exec ls {} \; -exec cat {} \;
then convert this to a perl script with find2perl which comes with perl.

RE: Extract some file n move the files


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.


RE: Extract some file n move the files

Ok .... Below is my script:

opendir(DIR,".") || die "Serious dainbramage: $!"  #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, ">cf.txt");                                      
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 "@filelist";                                    # so , the cf.txt will have the p.. file
 }                                                              # and date of the p*** file
close (CF);                                       
open (MAIL, "| /usr/sbin/sendmail -t ");          # then i will mail it to somebody
print MAIL qq~To: "Kenix" <kenix_neo@yahoo.com>\
print MAIL qq~\nFrom: "Kenix" <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~.~;                                 
#unlink @allfiles;                                                   # remove the p* file in my
                                                                            #current directory

hmmm ... above is my script and explaination.  The result i get is a "blank" 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):

grep -l CONNECT p* > c                               
for i in `cat c`                                     
   xx $i >> /apps/xxx/abc/laser/work/back/dcon
     mv $i /apps/xxx/abc/laser/work/back       

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

while (<>){                         
  printf "%s\n", 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 .................

RE: Extract some file n move the files

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.

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

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

foreach $file (@allfiles)
    open(IPF,"<$file") or die "Failed to open IPF, $!\n";
    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 " $file\t$date\n";
    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

RE: Extract some file n move the files

Thanks goBoating .... i will work on ur script.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close