unix shell: replace by dictionary

后端 未结 3 651
走了就别回头了
走了就别回头了 2020-12-15 14:59

I have file which contains some data, like this

2011-01-02 100100 1 
2011-01-02 100200 0
2011-01-02 100199 3
2011-01-02 100235 4

and have s

3条回答
  •  一个人的身影
    2020-12-15 15:39

    I hope perl is ok too:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    open(DICT, 'dict.txt') or die;
    my %dict = %{{ map { my ($id, $name) = split; $id => $name } () }};
    close(DICT);
    
    my %level = ( 0 => "warning", 
                  1 => "error",
                  2 => "critical" );
    
    open(EVTS, 'events.txt') or die;
    
    while ()
    {
        my ($d, $i, $l) = split;
        $i = $dict{$i}  || $i;  # lookup
        $l = $level{$l} || $l;  # lookup 
        print "$d\t$i\t$l\n";
    }
    

    Output:

    $ ./script.pl
    2011-01-02      Event1  error
    2011-01-02      Event2  warning
    2011-01-02      Event3  3
    2011-01-02      Event4  4
    

提交回复
热议问题