简体   繁体   中英

How to check a condition and then print specific fields in lines before the line with the condition using awk?

I have a file with the following output:

58.752391 0.000  1 1  6.152565 2.757839 14.558406 0.000000 2.156979  0.000000 0.000000  0 0 0  1
16.089417316313 0.000000000000 6.171292860915 2.757949885550  -150168 0
6.953218e-310 0.000000e+00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0 0 0 0 0.000000   0.000000 0.000000  0
0.000000 0.000000 0 0 0 0 0.000000   0.000000 0.000000  0
-1.000000 -1.000000
0 

14034.172996 0.000  13 13  1.107936 1.107936 -1.000000 -1.000000 -1.000000  23.670258 34.172995  0 0 0  0
3085.963203076240 0.667625281751 10.905159250868 8.915904022910  -150168 639
6.953218e-310 0.000000e+00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3.120454 8.844320 8 1 127.895 0 23.670258   1.107936 0.000000  0
1.107936 8.390152 13 5 1e+50 0 34.172995   1.107936 0.000000  0
-1.000000 -1.000000
3 CE1(5-1;8-1) SN1 SN2 

and so on...

I would like to check whether the two numbers in the 6th line (and then every 8th line) are different than -1.000000. If so, depending on which (the first, second or both) one is different, I'd like to produce the following output:

1) The first number is different than -1.000000:

this different number, 4 lines before this number - the third column (in this example 6.171292860915 if the number was different than -1.000000), 2 lines before this number - 8th column (in this example 0.000000), the same line - 7th column, the line after this number

2) The second number is different than -1.000000:

this different number, 4 lines before this number - the fourth column, 1 line before this number - 8th column, the same line - 7th column, the line after this number

3) The first and second number are different than -1.000000:

Output from 1) Output from 2)

I know how to use sed to extract these lines where -1.000000 in my example are. Now I think I should include some awk's ifs in my sed command to check whether the line contains -1.000000 or other numbers and then print what I need. I have no idea, however, how to refer to lines before that which is being examined by sed. I'd be grateful for any help or clues. Thank you!

Perl to the rescue:

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

# Read in "paragraph mode".
$/ = q();

# Auto add newlines.
$\ = "\n";

while (<>) {
    my @lines = map [ split ], split /\n/;  # Create an array of arrays.
    if (-1 != $lines[5][0]) {
        print join ' ', $lines[5][0], $lines[1][2], $lines[3][7],
                        $lines[3][6], @{ $lines[6] };
    }
    if (-1 != $lines[5][1]) {
        print join ' ', $lines[5][1], $lines[1][3], $lines[4][7],
                        $lines[4][6], @{ $lines[6] };
    }
}

In awk, the variable NR holds the line number, so the expression

NR % 8 == 6 { ..... }

will select lines 6, 14, 22 and so on. You only need a counter, but variables are automatically initialized to zero, so you get the sequence number (1 for line 6, 2 for line 14 and so on) with an expression like this

++seqno

Hope that helps....

one more solution in awk

awk '{if(NR%8==2){a=$3;b=$4};if(NR%8==4){a=$8","$7","a};if(NR%8==5){b=$8","$7","b};if(NR%8==6){c=$1;d=$2};if(NR%8==7){if(c!=-1.000000)print a,$0;if(d!=-1.000000)print b,$0 }}' inputfilename

if I break the above command as below

awk '{
     if(NR%8==2){a=$3;b=$4};
     if(NR%8==4){a=$8","$7","a};
     if(NR%8==5){b=$8","$7","b};
     if(NR%8==6){c=$1;d=$2};
     if(NR%8==7){
               if(c!=-1.000000)print a,$0;
               if(d!=-1.000000)print b,$0 
                }
     }' inputfilename

I am storing the output details in a and b from the beginning while reading file line by line like NR%8==2 is line 2 , NR%8==4 is line 4 of file and so on. At the 7th line (NR%8==7), I am checking for values c and d stored from line 6 (NR%8==6), if c and d has mismatch values we will print the output with 7th line content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM