Select only the last line from the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select only the last line from the pattern
# 8  
Old 08-05-2012
You could put together your info and the hints given in this thread:
Code:
grep "E(" 6dialkene.out |tail -3|cut -d" " -f8

if you need the energies by themselves. If you need the whole line, drop the cut filter.


grep -i "input orientation" will output 16 occurrences and data for these is screens and screens of data. Which ones do you need?
# 9  
Old 08-05-2012
Quote:
Originally Posted by RudiC
Code:
grep "E(" 6dialkene.out |tail -3|cut -d" " -f8

filter.
This definitely works, but i suspect the printing routine to give formatted output. If the number of blanks varies because, for instance, a two-digit value has one leading space more than a 3-digit value ("-234" vs " -23" - don't know if they are possible at all) your line might fail. I suggest a slight modification therefore (replace "<spc>" by a blank and "<tab>" by a literal tab character):

Code:
sed -n '/E(/ s/[<spc><tab>][<spc><tab>]*/<spc>/gp' | tail -3 | cut -d' ' -f4

The sed command does the same as the grep but replaces all consecutive whitespace by one space. This will work regardless of the number of leading whitespace.

I hope this helps.

bakunin
# 10  
Old 08-05-2012
Basically I would just need to create one script for each of the following outputs. Thanks a million for the help, it is really useful for me

output 1 (Last energy):
Code:
SCF Done:  E(RB3LYP) =  -234.626900695     A.U. after    7 cycles

output 2 (-1 and -2 energies):
Code:
 SCF Done:  E(RB3LYP) =  -234.626899214     A.U. after    8 cycles
 SCF Done:  E(RB3LYP) =  -234.626900474     A.U. after    8 cycles

Output 3 (Last "input orientation"):
Input orientation:

Code:
      1          6           0       -0.399347   -5.262276    0.924477
      2          1           0        0.036285   -6.254346    0.851459
      3          1           0       -1.018756   -5.065127    1.796487
      4          6           0       -0.191339   -4.333337   -0.008437
      5          1           0        0.440298   -4.576219   -0.864753
      6          6           0       -0.747870   -2.936770    0.017425
      7          1           0       -1.402957   -2.809077    0.886816
      8          1           0       -1.372645   -2.775463   -0.873590
      9          6           0        0.357075   -1.853859    0.042264
     10          1           0        1.026052   -2.019980   -0.815204
     11          1           0        0.967516   -1.976710    0.944220
     12          6           0       -0.196861   -0.457411   -0.019460
     13          1           0       -0.788822   -0.220813   -0.905375
     14          6           0       -0.030242    0.478629    0.914665
     15          1           0        0.549025    0.287676    1.815162
     16          1           0       -0.460889    1.470485    0.814332

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by bakunin; 08-05-2012 at 04:05 PM..
# 11  
Old 08-05-2012
Quote:
Originally Posted by bakunin
This definitely works, but i suspect the printing routine to give formatted output. If the number of blanks varies because, for instance, a two-digit value has one leading space more than a 3-digit value ("-234" vs " -23" - don't know if they are possible at all) your line might fail. I suggest a slight modification therefore (replace "<spc>" by a blank and "<tab>" by a literal tab character):
...
bakunin
@bakunin: You're absolutely right re. the unreliable number of spaces (e.g. one would ecpect the energies to be in field 6 rather than -f8 for cut). I kept it on the simple side, your sed proposal is much safer here (would be field 6 then, btw).

@hmartine1983: as you obviously only want entire lines, try this (shamelessly borrowing aashish.sharma8's proposals):
output 1:
Code:
grep "E(" 6dialkene.out |tail -1
 SCF Done:  E(RB3LYP) =  -234.626900695     A.U. after    7 cycles

output 2:
Code:
grep "E(" 6dialkene.out |tail -3|head -2
 SCF Done:  E(RB3LYP) =  -234.626899214     A.U. after    8 cycles
 SCF Done:  E(RB3LYP) =  -234.626900474     A.U. after    8 cycles

output 3:
Code:
grep -A20 "Input orientation" 6dialkene.out |tail -16

will result in the 16 lines requested.
These rely heavily on data structures being fixed as shown in your example; any deviation might need more sophisticated analysis in the way bakunin pointed out.

Last edited by RudiC; 08-05-2012 at 04:40 PM..
# 12  
Old 08-05-2012
Thanks a lot to everyone, very helpful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Modifying the shell script to select pattern

Hello, I have script which work 70% of the desired task , the output from script.sh is following , however the desired output I require is following . Any piece of suggestion would be great.. thanks in advance, emily #!/bin/bash ... (8 Replies)
Discussion started by: emily
8 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

Select everything between first and last occurrence of same pattern

Greetings, I am writing a script which requires as a part, selecting all the lines between the first and last occurrence of a pattern. I have an nawk alternative that is working. But thre should be a generic script that should run on all os viz, linux, sun , aix. The awk script that i... (25 Replies)
Discussion started by: usha rao
25 Replies

6. Shell Programming and Scripting

Select a pattern from file

Hi, I have a requirement to select only a specific pattern from a flat file and delete its occurance. For eg. If my file contains : <A1>1234</A1> <A2>5678</A2> <ABC>1234</ABC> <A3>0987</A3> Then, i want to delete <ABC>1234</ABC> from the file and have the contents as :-... (7 Replies)
Discussion started by: DTechBuddy
7 Replies

7. Shell Programming and Scripting

select some text from a test dependng on pattern

I have some absolute file location $INSTALL_BASEPATH/onereview-5.0/resources/commons-messages/commonmessages_default.properties $INSTALL_BASEPATH/onereview-5.0/orv-deploy/config-console.war/WEB-INF/classes/com/connectiva/configuration/console/resource/configurationBundle.properties I need to... (3 Replies)
Discussion started by: mnmonu
3 Replies

8. Shell Programming and Scripting

Select everything before a pattern

Hi I have i doubt, actually i have to select everything before a word(pattern).For that i am using sed i am using the below line of code but it is not working i am getting a blank instead.. sed -n '/regexp/{g;1!p;};h' file1 Can anyone help? Thanks (15 Replies)
Discussion started by: usha rao
15 Replies

9. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

10. Shell Programming and Scripting

extract/select pattern from input

Hey, examples of the input (text line): /bla/blMOasdn234.adanif24/blabla.rar /bla/blMOasdn234.adanif24/blabla23124.bin /bla/bla/bla/bla/bla/bla.bin and what I need to do is extract/select only the dir path so the output would be: /bla/blMOasdn234.adanif24/ /bla/blMOasdn234.adanif24/... (4 Replies)
Discussion started by: TehOne
4 Replies
Login or Register to Ask a Question