处理多行文本文件以单行打印 [英] process multiple lines text file to print in single line

查看:26
本文介绍了处理多行文本文件以单行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 TCL 脚本的新手.只是想从这里的专家那里得到一些建议.

I am newbie in TCL scripting. Just wish to get some advice from experts here.

我希望按照以下格式处理报告.我想打印没有标题的报告,只有一行 -

I wish to process the report with format as below. I am thinking to print the report without the header and with just single line like -

Connections for net 'pmg_ccu_ot2_tam_50mhz_sel_xainfwh' :: Driver a_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Output Pin (a_par) :: Load  b_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Input Pin (b_par)
Connections for net 'pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2' :: Driver d_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2 Output Pin (d_par) :: Load  e_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2 Input Pin (e_par), f_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (f_par)  
<more . . .>

如果有人能给我一些启发和想法,我真的很感激.非常感谢!

Really appreciate if anyone can give some light and idea to me. Thanks much!

****************************************
Report : net
        -connections
Design : soc
Version: G-2012.06-SP2
Date   : Sun Apr  7 22:56:33 2013
****************************************


Connections for net `pmg_ccu_ot2_tam_50mhz_sel_xainfwh`:

Driver Pins         Type                
------------        ----------------    
a_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Output Pin (a_par)

Load Pins           Type                
------------        ----------------    
b_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Input Pin (b_par)  

1  


Connections for net `pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2`:

Driver Pins         Type                
------------        ----------------    
d_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2 Output Pin (d_par)

Load Pins           Type                
------------        ----------------    
e_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (e_par)
f_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (f_par)  

1

<more . . .>

推荐答案

如果我正确理解您的要求,我会这样做:

If I'm understanding your requirements correctly, I would do something like this:

输入文件包含:

****************************************
Report : net
        -connections
Design : soc
Version: G-2012.06-SP2
Date   : Sun Apr  7 22:56:33 2013
****************************************


Connections for net `pmg_ccu_ot2_tam_50mhz_sel_xainfwh`:

Driver Pins         Type                
------------        ----------------    
a_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Output Pin (a_par)

Load Pins           Type                
------------        ----------------    
b_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Input Pin (b_par)  

1  


Connections for net `pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2`:

Driver Pins         Type                
------------        ----------------    
d_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2 Output Pin (d_par)

Load Pins           Type                
------------        ----------------    
e_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (e_par)
f_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (f_par)  

1

<more . . .>

脚本:

# Read file containing input
set inputfile [open "inputfile.txt" r]
# File where output will be written in
set outputfile [open "outputfile.txt" w]

# $type will contain the Pin Type and $outputline will contain the final
# line to be written
set type ""
set outputline ""

# Read each line in the inputfile
while {[gets $inputfile line] != -1} {
    # If first word in line is "Connections" execute
    if {[lindex [split $line " "] 0] == "Connections"} {
        # If $outputline not empty while in this if, it means that we have
        # reached a new connection. So print out the current connection
        # after joining all its elements by " :: " and reset the output line.
        if {$outputline != ""} {
            puts $outputfile [join $outputline " :: "]
            set outputline ""
        }
        # Replacing the ` by ' and removing the end ":" and then putting
        # the full thing into $output line as an element of a list
        regsub -all {\`} $line "\'" newline
        set newline [string trimright $newline "\:"]
        lappend outputline $newline
    }
    # If there is "Pins" in the line, this means we have a header. The regexp
    # captures the pin type by looking for any alphanumeric characters before
    # "Pins"
    regexp -- {([\w]+) Pins} $line - type

    # If there is " Pin " in the line, it means we are in a row of the
    # different 'Pins' type. Here, we put the while line after removing
    # the extra white spaces into the $outputline list.
    if {[regexp -- { Pin } $line]} {
        lappend outputline "$type [string trim $line]"
    }
}
# We reached the end of the document. So, add the last line into the outputfile.
puts $outputfile [join $outputline " :: "]

# Close the files we opened.
close $inputfile
close $outputfile

输出:

Connections for net 'pmg_ccu_ot2_tam_50mhz_sel_xainfwh' :: Driver a_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Output Pin (a_par) :: Load b_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh Input Pin (b_par)
Connections for net 'pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2' :: Driver d_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_2 Output Pin (d_par) :: Load e_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (e_par) :: Load f_par/pmg_ccu_ot2_tam_50mhz_sel_xainfwh_3 Input Pin (f_par)

此脚本将采用驱动程序类型"或加载类型"的任意顺序(您也可以添加新类型).您输入的内容是否还有其他可能的变化?

This script will take 'Driver Type' or 'Load Type' in any order they come (and you can add new types as well). Are there any other possible variations in your in your input?

您可以将 switch 块更改为此,它将采用任何类型的 Pins

You can change the switch block into this and it will take any type of Pins

if {[regexp -- {([\w]+) Pins} $line - type]} {
    set counter 0
}

re在正则表达式中添加了 --.

re Added -- to the regexp.

rere有问题的匹配编辑.

rere Matched edit in question.

这篇关于处理多行文本文件以单行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆