LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sed/awk/grep for multiple line data (http://www.linuxquestions.org/questions/programming-9/sed-awk-grep-for-multiple-line-data-903944/)

Coiby 09-20-2011 12:01 AM

sed/awk/grep for multiple line data
 
Hi, all!
I want to filter out some text from the following style of file.

Code:

...
entering subroutine stress ...

          total  stress  (Ry/bohr**3)                  (kbar)    P=    9.71
  0.00006500  0.00000000  0.00000000          9.56      0.00      0.00
  0.00000000  0.00006500  0.00000000          0.00      9.56      0.00
  0.00000000  0.00000000  0.00006797          0.00      0.00    10.00


    Wentzcovitch Damped Dynamics: convergence achieved, Efinal=  -45.70774991

------------------------------------------------------------------------

    Final estimate of lattice vectors (input alat units)
  0.995584463  0.000000000  0.000000000
  -0.497792232  0.862201437  0.000000000
  0.000000000  0.000000000  2.635053669
  final unit-cell volume =    226.1303 (a.u.)^3
  input alat =      4.6412 (a.u.)
Begin final coordinates
    new unit-cell volume =    226.13029 a.u.^3 (    33.50905 Ang^3 )

CELL_PARAMETERS (alat=  4.64117000)
  0.995584463  0.000000000  0.000000000
  -0.497792232  0.862201437  0.000000000
  0.000000000  0.000000000  2.635053669

ATOMIC_POSITIONS (crystal)
C        0.000000000  0.000000000  0.000000000
C        0.333333333  0.666666666  0.000000000
C        0.000000000  0.000000000  0.500000000
C        0.666666666  0.333333333  0.500000000
End final coordinates
...

I want to get these 3 lines of data just after the line "Final estimate of lattice vectors..."(These is only one "Final" in the file).
Code:

0.995584463  0.000000000  0.000000000
  -0.497792232  0.862201437  0.000000000
  0.000000000  0.000000000  2.635053669

Thanks very much!

Jerre Cope 09-20-2011 12:43 AM

I know you said awk, but Perl is easier to finish with a job well done.
 
#!/usr/bin/perl -wU

use strict;
use IO::Handle;

if ($#ARGV != 0){
die( "usage: $0 FinalTextFile\n ");
};
my $FinalFile = $ARGV[0];
open (INFILE,"<",$FinalFile) || die ("$FinalFile not found.$?");

my $count = -2;

while ( <INFILE> ) {
$count = -1 if ( /Final estimate of lattice/ );
$count +=1 if $count > -2 ;
print $_ if ( $count > 0 && $count < 4 )
}
close INFILE;

colucix 09-20-2011 12:58 AM

Code:

grep -A3 Final file

Coiby 09-20-2011 06:15 AM

Thanks!
 
Quote:

Originally Posted by colucix (Post 4476808)
Code:

grep -A3 Final file

Code:

grep -A3 Final file|tail -n3
is exactly what I want. Thanks!

Coiby 09-20-2011 06:16 AM

Thanks!
 
Quote:

Originally Posted by Jerre Cope (Post 4476798)
#!/usr/bin/perl -wU

use strict;
use IO::Handle;

if ($#ARGV != 0){
die( "usage: $0 FinalTextFile\n ");
};
my $FinalFile = $ARGV[0];
open (INFILE,"<",$FinalFile) || die ("$FinalFile not found.$?");

my $count = -2;

while ( <INFILE> ) {
$count = -1 if ( /Final estimate of lattice/ );
$count +=1 if $count > -2 ;
print $_ if ( $count > 0 && $count < 4 )
}
close INFILE;

Thanks! Perl is cool!


All times are GMT -5. The time now is 06:41 AM.