-1
51 0 0 5  
0 0.0 0.0 0  
1 2 3 4 5 6 7 8  
1, 2  
0.998 0.567  
3, 2 Rs12345  
0.7 0.2  
3, 2 Rs31256  
0.56 0.311  
3, 2 Rs25691  
0 0  
012.1313010310 0.1213212 0.2121331321
0.0121654564 0.254564564 0.25678646 
0.02154 0.2485674354 0.2434  

The resulting output should look like this

3, 2 Rs12345    
0.7 0.2  
3, 2 Rs31256  
0.56 0.311  
3, 2 Rs25691

I used the sed to achieve the desired result

sed -i -e '1,5d;/0 0/,$d' filename

This did not work. I am dealing with multiple files like this that have different lengths in the number of lines. Therefore, I will have to somehow get rid of the "0 0" and every line after that (very end of data)

2
  • 1
    I'm afraid you will have to reword this; I can't understand what you want, or how the result doesn't match your expectation ...
    – tink
    Feb 18, 2016 at 21:29
  • @tink I only want the data that is after the first 5 lines and before the "0 0". Sorry for the confusion Feb 18, 2016 at 21:40

3 Answers 3

0

You had it ALMOST right ...

sed -e '1,5d;/^0 0/,$d' genetics 
3, 2 Rs12345  
0.7 0.2  
3, 2 Rs31256  
0.56 0.311  
3, 2 Rs25691

Just add the -i back in if this is what you want

9
  • @don_crissti, that may well be, but there's nothing to say that any of their other files doesn't have '0 0' on one of the lines that have valid data in front of it.
    – tink
    Feb 18, 2016 at 21:53
  • @don_crissti: and I didn't say the anchor was what made it right; it's the same ' you added into the original post ;}
    – tink
    Feb 18, 2016 at 22:21
  • don - they didn't say in what way it didn't produce the desired result. It may well have been a sed error?
    – tink
    Feb 18, 2016 at 22:33
  • @don_crissti that may well be, but there's nothing to say that any of their other files doesn't have '0 0' on one of the lines that have valid data in front of it. <- this is very well a possibility Feb 18, 2016 at 22:40
  • @tink your solution worked when I added the "-i" back in. Thanks so much Feb 18, 2016 at 22:43
0

perl has a nice feature, called the range operator. It lets you do something like this:

#!/usr/bin/env perl

use strict;
use warnings;

while ( <DATA> ) { 
    print if 6 ... /0 0/;
}

__DATA__
51 0 0 5  
0 0.0 0.0 0  
1 2 3 4 5 6 7 8  
1, 2  
0.998 0.567  
3, 2 Rs12345  
0.7 0.2  
3, 2 Rs31256  
0.56 0.311  
3, 2 Rs25691  
0 0  
012.1313010310 0.1213212 0.2121331321
0.0121654564 0.254564564 0.25678646 
0.02154 0.2485674354 0.2434  

You can test for being between two regular expressions, line numbers or a mix.

Although, looking at it, perhaps it's simpler to just:

while ( <DATA> ) { 
    next unless $. > 5; 
    last if /0 0/;
    print;
}

Handily - you can do this with the -p flag, to make it a one liner:

perl -pe 'next unless $. > 5; last if /0 0/' your_file

Because we're using last it aborts processing (and stops reading the file).

-p is a perl option that make it behave a bit like sed - it transforms a line, and prints the line afterwards.

0
sed -i -e '1,5d' -e '/0 0/,$d

if the line containing "0 0" is not at a fixed location.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .