CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to print all the file name irrespective of the file extension?

 



iphone
User

May 31, 2010, 7:26 PM

Post #1 of 18 (2101 views)
How to print all the file name irrespective of the file extension? Can't Post

Hi friends,

I am trying to all the lines that has a ".c" extension using the following script,for some reason the the matching pattern "$_ =~ /^.*\.c\z/s"doesn't seem to work.Can some pls help?When i run this script nothing gets printed

use strict;

open FILE, "<features.txt";
my @line = <FILE>;

#print "Lined that matched $find\n";
foreach (@line) {
chomp(@line);
if ($_ =~ /^.*\.c\z/s)
{ print "in f";

print "$_\n";
}
}
close (FILE);


(This post was edited by iphone on Aug 22, 2010, 11:55 PM)


shawnhcorey
Enthusiast


Jun 1, 2010, 4:45 AM

Post #2 of 18 (2086 views)
Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

You're looking for a .c at the end of the line? If not, try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

open my $features_fh, '<', 'features.txt' or die "could not open featues.txt: $!\n";

while( <$features_fh> ){
if( m{ \. c \b }msx ){
print "in f: $_";
}
}

close $features_fh or die "could not close featues.txt: $!\n";


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


iphone
User

Aug 2, 2010, 4:27 PM

Post #3 of 18 (2020 views)
Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

I need to remove the trailing version numbers in the below output,#3,#33,#2 etc.How do i change my script?

OUTPUT:

core_ui.h#3
AEEPosDetExt.h#22
AEEPosDetExt_priv.h#2
GpsDsTest.c#2


Quote


#!/usr/bin/perl

use strict;
use warnings;

open my $features_fh, '<', 'a.glf' or die "could not open featues.txt: $!\n";

while( <$features_fh> ){

my @values = split(/\//,$_);
foreach my $val (@values) {
if(( $val =~m { \. c \b }msx) || ($val =~m { \. h \b }msx )){
$/=#;
chomp($val);
print "$val";
}
}
}

Quote


BillKSmith
Veteran

Aug 3, 2010, 9:24 AM

Post #4 of 18 (2007 views)
Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

It looks like you are attempting to remove path as well as version info.

Is this what you want?


Code
use strict;  
use warnings;
while( <DATA> ){
(my $file_name) = /([^\\\/]+ \. [ch]) [\s#]/xms;
next if !$file_name;
print $file_name, "\n";
}
__END__
c:\here\there\anywhere\name.c#2 # print 'name.c'
c:\here\there\anywhere\macro.h # print 'macro.h'
app.c#2 # print 'app.c'
script.pl#2 # do not print
test.h#2
c:\here\there\anywhere\name.cxx#2 # do not print
here/foo.html#2 # do not print
here/foo.h#2 # print 'foo.h'

Good Luck,
Bill


iphone
User

Aug 3, 2010, 11:13 AM

Post #5 of 18 (2003 views)
Re: [BillKSmith] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

Thanks a lot Bill,that's exaclty what I want but not complete

1.Basically,I need to save all of these files into a text file after removing the path as well as version info
2.Compare the above text file with another file2 & file3 and print the matching files from file2 and file3.

Can you please help in achieving this?


(This post was edited by iphone on Aug 3, 2010, 11:27 AM)


BillKSmith
Veteran

Aug 3, 2010, 1:42 PM

Post #6 of 18 (1995 views)
Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

I assume:

  • file1, file2 and File3 are all in the same format as <DATA>
    (without comments) from previous post.


  • all lines end with newline


  • From file2 and file3, you wish to print all lines
    which contain any of the file names extracted from
    file1. Path and version may be different.



  • untested

    Code
    use strict;  
    use warnings;
    open my $file2, '<', 'file2' or die 'Cannot open file2';
    my @file_2 = <$file2>;
    close $file2;
    open my $file3, '<', 'file3' or die 'Cannot open file2';
    my @file_3 = <$file3>;
    close $file3;

    open my $file1, '<', 'file1' or die 'Cannot open file2';
    while( <$file1> ){
    (my $file_name) = /([^\\\/]+ \. [ch]) [\s#]/xms;
    next if !$file_name;
    print grep /$file_name/, (@file_2, @file_3);
    }
    close $file1;

    Good Luck,
    Bill


    iphone
    User

    Aug 3, 2010, 8:41 PM

    Post #7 of 18 (1980 views)
    Re: [BillKSmith] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    1.print grep /$file_name/, (@file_2, @file_3);-->This prints all the file names with task.c,for example dstask.c,onctask.c etc.Can you please help in printing only the exact file

    2.(my $file_name) = /([^\\\/]+ \. [ch]) [\s#]/xms; -->This is currently looking for only .c,.h,can you please tell how do I change it to if I have a file with .cfg extension

    In Reply To


    BillKSmith
    Veteran

    Aug 4, 2010, 10:30 AM

    Post #8 of 18 (1971 views)
    Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    1. I am still not sure of your requirement. This will only print the file names from file_1 if they also appear in either file_2 or in file_3.


    Code
    print $file_name if grep /$file_name/, (@file_2, @file_3);



    2. Change [ch] to (?:cfg|c|h)
    Good Luck,
    Bill


    iphone
    User

    Aug 4, 2010, 12:26 PM

    Post #9 of 18 (1969 views)
    Re: [BillKSmith] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    Let me explain clearly on my requirement for #1.

    print $file_name if grep /$file_name/, (@file_2, @file_3);-->Here we are grepping for the file name in file2 and file3.So,if a file name is dstask.c in file2 and file3 and if we are grepping for a file task.c in file2 and file3,dstask.c is going to get printed.My requirement is I only need to print files with name task.c in file2 and file3,butnot dstaskc.

    Thanks


    (This post was edited by iphone on Aug 5, 2010, 11:12 AM)


    iphone
    User

    Aug 21, 2010, 11:34 AM

    Post #10 of 18 (1916 views)
    Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    I think if we add" \/ "before $file_name,it worked


    Code
    print grep /\/$file_name/, (@file_2, @file_3);



    (This post was edited by iphone on Aug 22, 2010, 2:01 AM)


    iphone
    User

    Aug 22, 2010, 2:09 AM

    Post #11 of 18 (1906 views)
    Post deleted by iphone [In reply to]

     


    iphone
    User

    Aug 23, 2010, 2:12 PM

    Post #12 of 18 (1862 views)
    Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    How do I change the below code to get only the file name irrespective of the file extension?Currently it only prints .c and .h

    Code
    (my $file_name) =  /([^\\\/]+ \. [ch]) [\s#]/xms;  

    Input:\ssma\products\services\utils\putlis_gsm.min;//source/modem/nas/services/rel/0987H1_200/src/putlis_gsm.min - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 2
    \ssma\products\services\utils\putlis_gsm.mak;//source/modem/nas/services/rel/0987H1_200/src/putlis_gsm.mak - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 2
    .\ssma\products\services\utils\sys_plmn_selection.c;//source/modem/nas/services/rel/0987H1_200/src/sys_plmn_selection.c - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 1
    .\ssma\products\services\utils\sys_plmn_selection.h;//source/modem/nas/services/rel/0987H1_200/inc/sys_plmn_selection.h - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 1

    OUTPUT:
    putlis_gsm.min
    putlis_gsm.mak
    sys_plmn_selection.c
    sys_plmn_selection.h



    (This post was edited by iphone on Aug 23, 2010, 2:46 PM)


    BillKSmith
    Veteran

    Aug 23, 2010, 4:07 PM

    Post #13 of 18 (1852 views)
    Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    Replace the regular expression.


    Code
    use strict; 
    use warnings;
    while (my $line = <DATA>){
    (my $file_name) = $line =~ /
    [\\\/] # Match but do not return a slash (or backslash)
    ( # Begin return
    \w+ # One or more word characters (the name)
    \. # one period
    \w{1,3} # one to three word characters (extension)
    ) # End of return
    ; # Match but do not return a semi-colen;
    /xms;
    print $file_name, "\n";
    }
    __END__
    \ssma\products\services\utils\putlis_gsm.min;//source/modem/nas/services/rel/0987H1_200/src/putlis_gsm.min - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 2
    \ssma\products\services\utils\putlis_gsm.mak;//source/modem/nas/services/rel/0987H1_200/src/putlis_gsm.mak - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 2
    .\ssma\products\services\utils\sys_plmn_selection.c;//source/modem/nas/services/rel/0987H1_200/src/sys_plmn_selection.c - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 1
    .\ssma\products\services\utils\sys_plmn_selection.h;//source/modem/nas/services/rel/0987H1_200/inc/sys_plmn_selection.h - VU_MIKE_ASA_SERVICES.00.00.12.00.06 : 1

    Good Luck,
    Bill


    iphone
    User

    Aug 23, 2010, 6:20 PM

    Post #14 of 18 (1844 views)
    Re: [BillKSmith] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post


    Code
    use strict;   
    use warnings;
    open my $file2, '<', 'file2.txt' or die 'Cannot open file2';
    my @file_2 = <$file2>;
    close $file2;
    open my $file3, '<', 'file3.txt' or die 'Cannot open file3';
    my @file_3 = <$file3>;
    close $file3;

    my $file = 'p4/file.ntp';
    open my $DATA, '<', $file or die "could not open '$file' $!";

    my %seen; # add this before loop

    while (my $line = <DATA>){
    (my $file_name) = $line =~ /
    [\\\/] # Match but do not return a slash (or backslash)
    ( # Begin return
    \w+ # One or more word characters (the name)
    \. # one period
    \w{1,3} # one to three word characters (extension)
    ) # End of return
    ; # Match but do not return a semi-colen;
    /xms;


    (my $VU_name) = /\s(VU\w+)/xms;
    next if (!$file_name);
    print $file_name, "\n";
    # print grep /\/$file_name/, (@file_2, @file_3);
    # next if ((!$VU_name) or ($seen{$VU_name}++));
    # print "$VU_name\n";
    # print grep /$VU_name/, (@file_2, @file_3);
    }
    close $DATA;


    OUTPUT:-

    Code
    Name "main::DATA" used only once: possible typo at orphan.pl line 16. 
    readline() on unopened filehandle DATA at orphan.pl line 16.



    BillKSmith
    Veteran

    Aug 23, 2010, 8:25 PM

    Post #15 of 18 (1839 views)
    Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    Change 'DATA' to '$DATA' in line 16
    Good Luck,
    Bill


    iphone
    User

    Aug 23, 2010, 10:54 PM

    Post #16 of 18 (1831 views)
    Post deleted by iphone [In reply to]

     


    iphone
    User

    Aug 25, 2010, 5:16 PM

    Post #17 of 18 (1801 views)
    Re: [BillKSmith] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    Hi Bill,

    In the above code.The following line seems to failing for the below input.

    Code
     
    (my $VU_name) = /\s(VU\w+)/xms;



    Code
    INPUT:- 
    .\msm\apps_proc\drivers\boot\ince_cache_mmu.s;//depot/asic/msmshared/apps_proc/drivers/boot/2_0/ince_cache_mmu.s - MSMSHARED_1024_SAPP_TOOL.02.00.35.01 : 2
    .\msm\build\msm\tool_debug.cmm;//depot/asic/main/build/cmm/tool_debug.cmm - VU_ROLE_TOOL_SMS_VERSION_MS.00.01.11 : 2



    DESIRED OUTPUT:-
    MSMSHARED_1024_SAPP_TOOL
    VU_ROLE_TOOL_SMS_VERSION_MS


    The following seems to be working but I am wondering if there is a better way


    Code
    ( my $VU_name ) = $line =~ /\-\s(\w+)/xms;



    (This post was edited by iphone on Aug 25, 2010, 6:32 PM)


    BillKSmith
    Veteran

    Aug 25, 2010, 8:36 PM

    Post #18 of 18 (1789 views)
    Re: [iphone] Trying to print all the lines in a file with a .c extension in it [In reply to] Can't Post

    It is simple. Only you can decide if it is right for all your data.
    Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives