You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Perl或Python提取指定信号并生成指定格式赋值文件?

Python 实现方案

咱们先从Python版本开始,代码清晰易懂,适合快速上手:

1. 提取input信号生成assign语句到file2

这段代码会读取file1的内容,筛选出所有input类型的信号(这里就是a、b、c),然后按照assign inst.信号名=;的格式写入file2:

import re

# 读取file1内容并处理
with open('file1', 'r') as f_in, open('file2', 'w') as f_out:
    for line in f_in:
        # 匹配input行,提取信号名
        match = re.match(r'^input\s+(\w+);', line.strip())
        if match:
            signal_name = match.group(1)
            # 写入指定格式的语句
            f_out.write(f'assign inst.{signal_name}=;\n')

2. 指定行范围查找字符串、提取内容追加功能

下面这个函数可以实现:在指定的行范围内查找包含特定标识的行,提取标识后的内容并追加到目标文件。比如你要在file1的第2-5行找包含wire的行,提取wire后面的信号名追加到file3:

def extract_and_append(source_file, target_file, start_line, end_line, marker):
    with open(source_file, 'r') as f_in, open(target_file, 'a') as f_out:
        current_line = 0
        for line in f_in:
            current_line += 1
            # 判断是否在指定行范围内
            if start_line <= current_line <= end_line:
                line_stripped = line.strip()
                # 查找包含指定标识的行
                if marker in line_stripped:
                    # 提取标识后的内容(这里假设标识和内容用空格分隔)
                    content = line_stripped.split(marker, 1)[1].strip().rstrip(';')
                    f_out.write(f'{content}\n')

# 示例调用:在file1的1-6行找包含"wire"的行,提取信号名追加到file3
extract_and_append('file1', 'file3', 1, 6, 'wire')

Perl 实现方案

如果习惯用Perl处理文本,下面的代码同样可以满足需求:

1. 提取input信号生成assign语句到file2

一行命令式的脚本就能搞定,利用Perl的正则匹配和文件处理:

#!/usr/bin/perl
use strict;
use warnings;

open(my $in_fh, '<', 'file1') or die "Cannot open file1: $!";
open(my $out_fh, '>', 'file2') or die "Cannot open file2: $!";

while (my $line = <$in_fh>) {
    chomp $line;
    # 匹配input行并提取信号名
    if ($line =~ /^input\s+(\w+);/) {
        print $out_fh "assign inst.$1=;\n";
    }
}

close $in_fh;
close $out_fh;

2. 指定行范围查找字符串、提取内容追加功能

这个Perl脚本可以实现指定行范围的查找和内容追加:

#!/usr/bin/perl
use strict;
use warnings;

sub extract_and_append {
    my ($source_file, $target_file, $start_line, $end_line, $marker) = @_;

    open(my $in_fh, '<', $source_file) or die "Cannot open $source_file: $!";
    open(my $out_fh, '>>', $target_file) or die "Cannot open $target_file: $!";

    while (my $line = <$in_fh>) {
        chomp $line;
        my $current_line = $.; # 获取当前行号
        next unless $current_line >= $start_line && $current_line <= $end_line;

        if ($line =~ /\Q$marker\E(.*)/) { # \Q转义特殊字符,避免正则冲突
            my $content = $1;
            $content =~ s/^\s+|\s+;$//g; # 去除前后空格和末尾的分号
            print $out_fh "$content\n";
        }
    }

    close $in_fh;
    close $out_fh;
}

# 示例调用:在file1的1-6行找包含"reg"的行,提取信号名追加到file3
extract_and_append('file1', 'file3', 1, 6, 'reg');

两种方案都可以直接运行,你可以根据自己的熟悉程度选择。如果需要调整匹配规则或者格式,只需要修改正则表达式部分就好啦~

内容的提问来源于stack exchange,提问作者Prashant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:00:38