【问题标题】:Numbering multi-line records continuously in a list在列表中连续编号多行记录
【发布时间】:2015-09-10 12:26:28
【问题描述】:

我试图在插入或删除记录后连续重新编号文件中的记录。该文件如下所示,包含 > 100 条记录。如果例如第 2 条记录已经被删除,后面的都需要重新编号,这样顺序就没有间隙了。任何想法如何做到这一点,例如bash 或 awk(或 perl)……如果只是理论上的话。

记录文件.txt:

#  filter rule1
FilterRule1.match_message_facility = MME_E
FilterRule1.match_message_process = -1
FilterRule1.match_message_host = -1
FilterRule1.not_matched_facility_to_log_to = MME_E
FilterRule1.max_time_since_last_match_secs = 300


# incoming files 
FilterRule2.match_message_facility = EXG
FilterRule2.match_message_event_severity = I
FilterRule2.match_message_host = -1
FilterRule2.not_matched_facility_to_log_to = EXG
FilterRule2.max_time_since_last_match_secs = 2000

# outgoing files  
FilterRule3.match_message_facility = EXG
FilterRule3.match_message_event_severity = I
FilterRule3.match_message_host = -1
FilterRule3.not_matched_facility_to_log_to = EXG
FilterRule3.max_time_since_last_match_secs = 14400

# outgoing files: included headers
FilterRule4.match_message_facility = EXG
FilterRule4.match_message_event_severity = I
FilterRule4.match_message_host = -1
FilterRule4.not_matched_facility_to_log_to = EXG
FilterRule4.max_time_since_last_match_secs = 900

...

【问题讨论】:

  • 这里的记录是什么?
  • 你尝试什么失败(以及哪个错误)?
  • @Vijay:一个 record 是具有 same FilterRuleX 编号的行,例如所有以 FilterRule2. 开头的行

标签: bash perl shell awk sed


【解决方案1】:

以“段落模式”读取输入,一次记录一条。将规则编号更改为您保留在变量中的值,为每条记录递增:

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

$/ = '';                    # Read in the "paragraph mode".
my $record_id = 1;
while (<>) {
    s/^FilterRule[0-9]+/FilterRule$record_id/gm;
    $record_id++;
    print;
}

【讨论】:

    【解决方案2】:

    求救!

    awk -vRS= -vORS="\n\n" '{gsub(/FilterRule[0-9]*/,"FilterRule"NR)}1'
    

    对记录从 1 开始连续编号。

    【讨论】:

    • 合并了cmets。
    • 信不信由你,不在-vvar=value 之间放置空格会使脚本特定于gawk,因此最好在-v RS= -v ORS='\n\n' 中放置一个空格,这样它就可以与任何awk。
    【解决方案3】:

    删除第三条记录:

    7> cat temp
    #  filter rule1
    Filterrule1.match_message_facility = MME_E
    Filterrule1.match_message_process = -1
    Filterrule1.match_message_host = -1
    Filterrule1.not_matched_facility_to_log_to = MME_E
    Filterrule1.max_time_since_last_match_secs = 300
    
    
    # incoming files
    Filterrule2.match_message_facility = EXG
    Filterrule2.match_message_event_severity = I
    Filterrule2.match_message_host = -1
    Filterrule2.not_matched_facility_to_log_to = EXG
    Filterrule2.max_time_since_last_match_secs = 2000
    
    
    # outgoing files: included headers
    Filterrule4.match_message_facility = EXG
    Filterrule4.match_message_event_severity = I
    Filterrule4.match_message_host = -1
    Filterrule4.not_matched_facility_to_log_to = EXG
    Filterrule4.max_time_since_last_match_secs = 900
    >
    

    运行命令:

    perl -pi -lne '{$i++ if(/^#/);s/Filterrule./Filterrule$i/g;}' temp
    

    编号现在将更改,并将按顺序排列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多