Extracting data between specific lines, multiple times | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Extracting data between specific lines, multiple times

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-29-2012
captainalright captainalright is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 2 January 2013, 3:29 PM EST
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Extracting data between specific lines, multiple times

I need help extracting specific lines in a text file. The file looks like this:


Code:
 POSITION                                       TOTAL-FORCE (eV/Angst)         
 -----------------------------------------------------------------------------------
      1.86126      1.86973      1.86972         0.000006      0.000006      0.000006
      1.83963      5.61435      5.60302        -0.000013      0.000019      0.000006
      ****A BUNCH MORE OF THESE NUMBERS****
      7.30218      7.59778     13.09455        -0.000012      0.000011      0.000016
     11.04119     11.33677     13.08985        -0.000021      0.000014      0.000014
 -----------------------------------------------------------------------------------

with about 50 of these sections. I want just the numbers between all the dashes for all the sections.

The trick is that the file has lots of these dashes (same number of dashes) separating a lot of data I don't need. I specifically need the data between the dashes with the first line of dashes preceded by that first line in the text (POSITION etc.)

Thanks!

Last edited by Scrutinizer; 12-29-2012 at 02:37 PM.. Reason: code tags
Sponsored Links
    #2  
Old 12-29-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 4:49 PM EST
Location: San Jose, CA, USA
Posts: 5,082
Thanks: 195
Thanked 1,701 Times in 1,444 Posts
I don't understand. Are you saying that you have ~50 sections in a file with each section looking like what you posted and you just want the last line of dashes to be removed from each section?

Is there something else at the start of the file or between sections that is supposed to be ignored?

Please show us more input data and show us what you want the output to be. (And, please use CODE tags.)
Sponsored Links
    #3  
Old 12-29-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 21 November 2014, 4:36 PM EST
Location: NM
Posts: 10,279
Thanks: 287
Thanked 814 Times in 756 Posts
'I just want the numbers':

Code:
 awk '$1 ~ /^[0-9]/'  inputfile > outputfile

print only lines where the first non-white character is a number.
    #4  
Old 12-29-2012
captainalright captainalright is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 2 January 2013, 3:29 PM EST
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts

Code:
**Lots of data I don't need before this***

POSITION                                       TOTAL-FORCE (eV/Angst)         
 -----------------------------------------------------------------------------------
      1.86126      1.86973      1.86972         0.000006      0.000006      0.000006
      1.83963      5.61435      5.60302        -0.000013      0.000019      0.000006
      ****A BUNCH MORE OF THESE NUMBERS****
      7.30218      7.59778     13.09455        -0.000012      0.000011      0.000016
     11.04119     11.33677     13.08985        -0.000021      0.000014      0.000014
 -----------------------------------------------------------------------------------
**Lots of data I don't need between these sections****

There's about 50 of these sections, with tons of information before and after the sections I don't need. I just want the numbers in between the dashes and nothing else outside it. Again, these sections start with POSITION (etc.) and the dashes and end with the dashes.

I want the output to look like:


Code:
      1.86126      1.86973      1.86972         0.000006      0.000006      0.000006
      1.83963      5.61435      5.60302        -0.000013      0.000019      0.000006
      ****A BUNCH MORE OF THESE NUMBERS****
      7.30218      7.59778     13.09455        -0.000012      0.000011      0.000016
     11.04119     11.33677     13.08985        -0.000021      0.000014      0.000014

with all the data for all 50 sections in a row with no spaces between the data.
Sponsored Links
    #5  
Old 12-29-2012
bvsarathi's Avatar
bvsarathi bvsarathi is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 12 February 2013, 4:29 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
perl -lne "if(/-----/.../-----/ and $_!~/----/){print}" joinl.txt


Last edited by Scott; 12-30-2012 at 01:40 AM.. Reason: Removed links
Sponsored Links
    #6  
Old 12-30-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 4:49 PM EST
Location: San Jose, CA, USA
Posts: 5,082
Thanks: 195
Thanked 1,701 Times in 1,444 Posts
If I understand the requirements correctly: print all lines between two lines that consist of exactly one space character followed by 83 hyphens if and only if the starting line of hyphens immediately follows a line starting with POSITION, and skip all other lines (including the hyphen lines and the POSITION line); the following awk script should do what you want:

Code:
awk '/^POSITION/ {
        dn = 1  # We expect a line of dashes next.
        next    # Skip to next line.
}
/^ -----------------------------------------------------------------------------------$/ {
        if(dn) {
                # This is a dashes line immediately after a POSITION line.
                dn = 0          # We are no longer looking for starting dashes.
                copy = 1        # Turn on copy mode.
        } else  copy = 0        # We found another line of dashes. Stop copying.
        next    # Skip to next line.
}       
dn {    dn = 0 # We expected a line of dashes but did not find it.  Reset.
        next    # Skip to next line.
}
copy {  print   # If we are in copy mode, print the line.
}' inputfile

If you are using a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk.
Sponsored Links
    #7  
Old 12-30-2012
captainalright captainalright is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 2 January 2013, 3:29 PM EST
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
I tried saving this and using chmod +x so I could run it (I'm not great with Unix so forgive me if I'm saying this wrong), and it ran, but there was no output.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting specific lines from data file palex UNIX for Advanced & Expert Users 5 08-04-2012 07:40 AM
Filtering data -extracting specific lines A-V UNIX for Dummies Questions & Answers 17 05-23-2012 08:33 AM
Extracting specific lines of data from a file and related lines of data based on a grep value range? Wynner Shell Programming and Scripting 3 05-18-2011 12:35 PM
Trying to read data multiple times coachr Shell Programming and Scripting 1 12-14-2006 08:19 PM
Trying to read data multiple times coachr Shell Programming and Scripting 0 12-06-2006 02:11 PM



All times are GMT -4. The time now is 10:27 AM.