grep 的正确语法:搜索字符串,复制上面两行并转置

grep 的正确语法:搜索字符串,复制上面两行并转置

我是 Linux 和 grep 的新手(2 天前),现在卡在这里。场景。我有超过 10 年的数据,我一直手动处理,直到我遇到了 grep。文件夹的形式是,/yyyy/mm/dd即第 1 天、第 2 天直到月底。我需要搜索特定字符串iteration 8。如果找到,则需要从所在iteration 8的行复制前 3 行。然后我需要将转置到输出文件中。这就是我试图解决我的困境的方法。由于无法转置,我试图拆分输出,然后稍后合并。请指导我解决这个案例。

 for file in /filepath/snc* #adding full path
     do
      echo $file
       grep -r " Mean" $file | awk '{print $1 " " $2}'> mean.txt # to enable single columns for ease of manipulation later
       grep -r " RMS" $file | awk '{print $1 " " $2}' > rms.txt
       grep -r " o-c" $file | awk '{print $3 " "$4}' > o-c.txt
       grep -rl "iteration 8" $file > iteration.txt # to verify that the files are the correct ones
      done

paste iteration.txt o-c.txt mean.txt rms.txt > daily-summary.txt #the output file must be in this specific order
grep "iteration 8" daily-summary.txt | awk '{print $3 " " $4 " " $5 " " $6 " " $7 " " $8}' >> monthly-summary-path.txt

#grep -3 "iteration 8" daily-summary.txt  >> monthly-summary-file.txt # two lines before

rm mean.txt rms.txt std.txt

示例输入文件:

            Mean    -78.6
            rms      1615
            o-c      1612.97456

iteration 8

示例输出文件:

year month day o-c         mean  rms
2015   12   12  1612.97456 -78.6 1615
2015   12   11  1525.36589 -78.0 1642

=======================   

答案1

这将创建单个月的报告:

#!/usr/bin/perl

use strict;
use warnings;

@ARGV == 1 || die($!);

my $realpath = `realpath $ARGV[0]`;
chomp($realpath);

opendir(my $dir, $realpath) || die($!);

my @files;

while(readdir($dir)) {
    -f "$realpath/$_" && push(@files, "$realpath/$_");
}

print("year\tmonth\tday\to-c\tmean\trms\n");

my @realpath_s = split("/", $realpath);

foreach my $file (sort(@files)) {
    open(my $in, $file) || die($!);

    while(<$in>) {
        if(/^\s*Mean/) {
            my @row;
            for(my $i = 0; $i < 3; $i++) {
                my @F = split(/\s/);
                push(@row, $F[2]);
                $_ = <$in>;
            }
            $_ = <$in>;
            my @F = split(/\s/);
            if($F[1] == 8) {
                $file =~ s/.*day//;
                print("$realpath_s[@realpath_s-2]\t$realpath_s[@realpath_s-1]\t$file\t$row[2]\t$row[0]\t$row[1]\n");
                last;
            }
        }
    }
}

print("\n=======================\n");

exit 0;

将其保存为~/script.pl,然后将路径传递给一个月的报告:

perl ~/script.pl /path/to/2015/12

输出将被打印到终端;您可以使用重定向将其重定向到文件:

perl ~/script.pl /path/to/2015/12 > ~/report_2015_12.txt

在 Bash 脚本中编写多个调用来创建年度/十年报告应该相当容易。

% tree
.
├── 2015
│   └── 12
│       ├── day1
│       ├── day2
│       └── day3
└── script.pl

2 directories, 4 files
% perl script.pl 2015/12
year    month   day o-c mean    rms
2015    12  1   1612.97456  -78.6   1615
2015    12  2   1612.97456  -79.6   1615
2015    12  3   1612.97456  -80.6   1615

=======================

在示例中,所有文件都2015/12包含iteration 8一行,因此每行都会打印一行。

相关内容