首页 文章

用于配置grep输出的Perl脚本

提问于
浏览
3

我有一个管道分隔的日志文件,其格式如下:

<date>  <time> | <fruit> | <color> | <num_1> | <num_2> | <num_3>

例如:

2013-03-27  23:01:52 | apple | green | 55 | 120 | 29
2013-03-27  23:01:56 | plumb | purple | 28 | 1 | 394
2013-03-27  23:01:59 | apple | red | 553 | 21 | 7822

我想编写一个perl脚本(虽然python或bash也可以接受) <date><time> 字段(第1列)以及 <num_1><num_2><num_3> ,具体取决于您为脚本提供的输入 . 因此,在上述信息上运行 perl extract.pl 2 会给你 <date><time><num_2>

2013-03-27  23:01:52 | 120
2013-03-27  23:01:56 | 1
2013-03-27  23:01:59 | 21

我尝试了以下但它似乎不起作用:

#!/usr/bin/perl

use warnings;
use strict;

my $col = $1;

print `grep "myapplog.txt" "m/_(\d{4})(\d\d)(\d\d)/ | $col"`

在这里,我将 col var设置为脚本的第一个arg,然后尝试打印匹配第一列的日期时间和欲望 <num_X> 列的grep . 有任何想法吗?提前致谢 .

2 回答

  • 1

    试试这个

    像你的愿望一样使用第一个参数(在 perl 中使用 @ARGV 数组,而不是 $1 ):

    #!/usr/bin/perl
    
    use warnings; use strict;
    use autodie; # No need to check open() errors
    
    $\ = "\n";   # output record separator (no need \n)
    
    # file-handle
    open my $fh, "<", "myapplog.txt";
    
    chomp(my $col = $ARGV[0]);
    
    die("Not an integer !\n") unless $col =~ /^\d+$/;
    
    # using the famous and magical <diamond> operator:
    while (<$fh>) {
        chomp;
        my @F = split /\|/; # splitting current line in @F array
        print join("|", @F[0,$col+2]); # join on a array slice
    }
    
    close $fh;
    
  • 4

    尝试在awk模式下使用perl

    $ perl -F'\|' -lane 'print $F[0]," | ", $F[4]' input
    2013-03-27  23:01:52  |  120 
    2013-03-27  23:01:56  |  1 
    2013-03-27  23:01:59  |  21
    

    纯awk:

    awk -F"|" '{print $1, "|", $5}' input
    

    纯粹的bash:

    #!/bin/bash
    
    IFS="|"
    
    while read -a ARRAY;
    do
        echo ${ARRAY[0]} "|" ${ARRAY[4]}
    done < input
    

    update

    传球,例如awk-solution的参数,用于确定要打印的女巫列,使用:

    $ awk -vcol="5" -F"|" '{print $1, "|", $col}' input
    

    在bash中,函数/脚本的第一个参数位于 $1 中,因此将其用作ARRAY的索引 .

    使用python比使用单行程更官方的东西:

    #!/usr/bin/env python
    
    import sys
    
    col = raw_input('which column to print? -> ')
    try:
        col = int(col)
    except ValueError:
        print >> sys.stderr, "That was no integer"
    
    with open("input") as fd:
        for line in fd:
            tmp = line.strip().split('|')
            print tmp[0], "|", tmp[col]
    

相关问题