简体   繁体   中英

extracting specific words (not keywords) from a log file

I am trying to extract a few words (as shown in expected output) from the following sample.txt and put them in a list. I am facing difficulty in extracting the correct fields. I have tried with my approach but it doesn't work for most cases. I prefer to do this using python, but open to other languages. Any pointers to other approaches is much appreciated.

sample.log

//*********************************************************************************
// update section
//*********************************************************************************
      for (i=0; i< models; i = i+1) begin:modelgen

     model_ip model_inst
         (
          .model_powerdown(model_powerdown),
          .mcg(model_powerdown),
          .lambda(_lambda[i])
          );
      assign fnl_verifier_lock = (tx_ready & rx_ready) ? &verifier_lock :1'b0;

   native_my_ip native_my_inst
     (
      .tx_analogreset(tx_analogreset),     
     //.unused_tx_parallel_data({1536{1'b0}})

      );

   // END Section I : 
   //*********************************************************************************
   resync 
     #(
       .INIT_VALUE (1)
       ) inst_reset_sync 
       (
    .clk    (tx_coreclkin),
    .reset  (!tx_ready), // tx_digitalreset from reset 
    .d      (1'b0),
    .q      (srst_tx_common  )
    );

expected output

model_ip
native_my_ip
resync

my try

import re

input_file = open("sample.log", "r")
result = []
for line in input_file:
    # need a more generic match condition to extract expected results 
    match_instantiation = re.match(r'\s(.*) ([a-zA-Z_0-9]+) ([a-zA-Z_0-9]+)_inst (.*)', line)


    if match_instantiation:
    print match_instantiation.group(1)
    result.append(match_instantiation.group(1))
    else:
        continue

You may need to read multiple lines at once to decide if the string is a module name or not.
Please try the following:

import re

input_file = open("sample.log", "r")
lines = input_file.read()   # reads all lines and store into a variable
input_file.close()
for m in re.finditer(r'^\s*([a-zA-Z_0-9]+)\s+([a-zA-Z_0-9]+\s+\(|#\()', lines, re.MULTILINE):
    print m.group(1)

which yields:

model_ip
native_my_ip
resync

The regex above looks ahead for the possible instance name or #( .

Hope this helps.

Use Perl

$ perl -0777 -ne ' while ( /^\s+((\w+)\s+(\S+)\s+\(\s+\.)|^\s+(\S+)\s+\#\(\s+/gmsx ) { print "$2$4\n" } ' sample.log
model_ip
native_my_ip
resync

$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM