LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-20-2011, 12:01 AM   #1
Coiby
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
Smile 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!
 
Old 09-20-2011, 12:43 AM   #2
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
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;
 
1 members found this post helpful.
Old 09-20-2011, 12:58 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Code:
grep -A3 Final file
 
1 members found this post helpful.
Old 09-20-2011, 06:15 AM   #4
Coiby
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Smile Thanks!

Quote:
Originally Posted by colucix View Post
Code:
grep -A3 Final file
Code:
grep -A3 Final file|tail -n3
is exactly what I want. Thanks!
 
Old 09-20-2011, 06:16 AM   #5
Coiby
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks!

Quote:
Originally Posted by Jerre Cope View Post
#!/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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using grep, awk or sed to find and cut data Jekikii Programming 8 04-21-2011 09:48 AM
Get data from multi lined text file using awk, sed or perl - grep & cut not upto par cam34 Programming 4 07-02-2010 03:10 AM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM


All times are GMT -5. The time now is 11:52 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration