Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

File modification on the fly

by mavericknik (Sexton)
on Aug 02, 2016 at 20:54 UTC ( #1169038=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I have a question about modifying a file I have on. It is a verilog netlist, so the lines are so:
DFFX1 k0_reg_184 ( .D(key_184 ), .CLK(clk), .QN(n18736) ); DFFX1 k0_reg_183 ( .D(key_183 ), .CLK(clk), .Q(k0_183 ), .QN(n65993) );
So some cells are contained in one line, whereas some cells span 2 lines(2nd one above). I'd like to move cells spanning 2 lines into one. I know the lines not containing cells will not have a capital X. So if i search for lines that dont match "X", I get all lines that should be appended to the line above it. How would I do this for a very large(~600000) line file? I plan to modify the netlist after this to make the changes I require.

Replies are listed 'Best First'.
Re: File modification on the fly (Verilog)
by toolic (Bishop) on Aug 02, 2016 at 21:13 UTC
    Parsing Verilog files can be tricky, and you should probably use a parser: Verilog-Perl. But, this might work for simple files:
    use warnings; use strict; # Change input record separator to parse whole Verilog lines local $/ = ';'; while (<DATA>) { s/\s+/ /g; # Convert all whitespace to single-space print "$_\n"; } __DATA__ DFFX1 k0_reg_184 ( .D(key_184 ), .CLK(clk), .QN(n18736) ); DFFX1 k0_reg_183 ( .D(key_183 ), .CLK(clk), .Q(k0_183 ), .QN(n65993) );

    Output:

    DFFX1 k0_reg_184 ( .D(key_184 ), .CLK(clk), .QN(n18736) ); DFFX1 k0_reg_183 ( .D(key_183 ), .CLK(clk), .Q(k0_183 ), .QN(n65993) +);
      Thank you so much for your help, I works perfectly. If I understand your script correctly, you are setting the line delimiter to a ";" and replacing all multiple spaces to single spaces? I have parsed the netlist using verilog-perl, but the time requirement seems to increase exponentially with number of cells. For older netlists with ~5k cells, the script takes about 12 hours. With my new netlist of about 450000 cells, I'm estimating a runtime of about 200 days which is unacceptable so I'm looking at alternative approaches.
        Thank you so much for your help
        You're welcome.
        If I understand your script correctly, you are setting the line delimiter to a ";"
        Yes.
        and replacing all multiple spaces to single spaces?
        \s refers to all types of whitespace; refer to perlre. The key here is that \s includes the newline character (\n). So, \s+ replaces multiple adjacent spaces, tabs and newlines. s///g changes all whitespace on a line.
        For older netlists with ~5k cells, the script takes about 12 hours.
        That is a surprisingly long time. Can you post your code for that?

        Depending on how your program works you can maybe gain some speed from using auto flush $| = 1;. OUTPUT_AUTOFLUSH

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-12-08 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?