Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

pattern matching

by changalrai (Initiate)
on Sep 21, 2010 at 22:14 UTC ( [id://861160]=perlquestion: print w/replies, xml ) Need Help??

changalrai has asked for the wisdom of the Perl Monks concerning the following question:

the file contains the information: name: karemaips01; name: karemadom02; name: karemadbo01; name: karemadom01.emea.pfizer.com; name: karemasss05; name: ka-lvsapp; name: emakars001.pfizer.com; name: ka-lvsn1; name: ka-efss; name: ka-lvsn2; name: karemafil-c; name: ka-lvscluco; name: ka-lvsdb; i need to capture each line data between :(colan) to ;(semicolan) for example: name: ka-lvscluco; i need only --> ka-lvscluco thanks in advance.

Replies are listed 'Best First'.
Re: pattern matching
by perlpie (Beadle) on Sep 21, 2010 at 22:23 UTC

    First, learn to use the formatting tools available. In particular, you should have noticed when you hit 'preview' that your text had run together. You probably wanted that in code tags.

    Second:

    #!/usr/bin/perl use strict; use warnings; while (<>) { print "found '$1'\n" if /:([^;]+)/; } __END__

    which when given the input file you describe via a call like

    ./foo.pl input_file

    will print

    found ' karemaips01' found ' karemadom02' found ' karemadbo01' found ' karemadom01.emea.pfizer.com' found ' karemasss05' found ' ka-lvsapp' found ' emakars001.pfizer.com' found ' ka-lvsn1' found ' ka-efss' found ' ka-lvsn2' found ' karemafil-c' found ' ka-lvscluco' found ' ka-lvsdb'
Re: pattern matching
by dasgar (Priest) on Sep 21, 2010 at 22:26 UTC

    First, if you would use <c></c> around your data, it would be easier for others to read and understand. At the moment, I can't tell if the data is all on one line or multiple lines in your file.

    The untested code below should give a start in the right direction. As for how to apply that regex to processing the file, you'll need to provide more information of the format of the file contents.

    use strict; use warnings; my $data = "name: ka-lvscluco;" $data =~ m/:\s+(.+?);/; my $extracted = $1; print "'$extracted' was pulled from '$data'\n";
Re: pattern matching
by changma_ha (Sexton) on Sep 22, 2010 at 09:29 UTC

    Hi changalrai......i hope my following code will help you. Suppose that your data is in SOPW.txt file .The the following code will retrieve what you wanted.

    #! /usr/bin/perl use strict; use warnings; open (my $fh, "<", "c:/SOPW.txt") or die "can't open the file :$!"; while (<$fh>){ chomp; if(m/\w+:(.*);/){ print "$1\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://861160]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-23 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found