Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Print string next to pattern matching?

by sumathigokul (Acolyte)
on May 05, 2015 at 06:29 UTC ( #1125674=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, i have a text file contains following lines...

2 INT_NET Net : c_c 2 INT_NET Net : b_c 2 INT_NET Net : a_c
Here, i want to extract string next to colon ':' i.e c_c, b_c and a_c and save them in another text file. How to print next to pattern matching?

I also want your comments on my approach of this problem. I have two files, one is .vhd file (which contains nets names as a_c, b_c and c_c)and another is designer.log file which lists high fanout nets of the design. i have to extract these high fanout nets from designer.log file and accordingly i have to modify those nets in .vhd file. For this problem, i wrote perl script which extract high fanout nets with its corresponding statements also as shown above. From this statements i want to extract those nets name alone??

Thank you all.

Replies are listed 'Best First'.
Re: Print string next to pattern matching?
by Anonymous Monk on May 05, 2015 at 06:52 UTC

      It prints/saves the whole statement which contains the pattern. But, i want the script to print only the string (not full line) next to colon (:).

Re: Print string next to pattern matching?
by AnomalousMonk (Bishop) on May 05, 2015 at 12:33 UTC

    Needs Perl version 5.10+ for  \K operator:

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; for my $s ( '2 INT_NET Net : c_c', '2 INT_NET Net : b_c', '2 INT_NET Net : a_c', ) { printf qq{'$s' -> }; my ($net_name) = $s =~ m{ : \s* \K \w+ }xmsg; print qq{'$net_name'}; } " '2 INT_NET Net : c_c' -> 'c_c' '2 INT_NET Net : b_c' -> 'b_c' '2 INT_NET Net : a_c' -> 'a_c'
    Please see perlre, perlretut, perlrequick, and Perlmonks regex tutorials.


    Give a man a fish:  <%-(-(-(-<

Re: Print string next to pattern matching?
by edimusrex (Monk) on May 05, 2015 at 14:52 UTC
    Try this

    #!/usr/bin/perl use strict; use warnings; open FILE,"<file.txt"; my @array = <FILE>; close FILE; foreach(@array){ if(/^.+?:\s(.+)$/) { open NEW_FILE, ">>new_file.txt"; print NEW_FILE "$1\n"; close NEW_FILE; } }

    The regex match should match anything after the colon and by using $1 uses that match. Use your own file names also.

      Hi edimusrex,

      The code is working exactly.... Thank you so much..

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-11-30 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?