How to print only lines in between patterns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print only lines in between patterns?
# 1  
Old 04-14-2013
How to print only lines in between patterns?

Hi,

I want to print only lines (green-italic lines) in between first and last strings in column 9.
there are different number of lines between each strings.


10 AUGUSTUS exon 4558 4669 . - . 10.g1
10 AUGUSTUS exon 8771 8889 . - . 10.g1
10 AUGUSTUS exon 16216 16284 . - . 10.g1

10 AUGUSTUS exon 17048 17135 . - . 10.g1
10 AUGUSTUS exon 17366 17525 . + . 10.g2
10 AUGUSTUS exon 19544 19603 . + . 10.g2
10 AUGUSTUS exon 20007 20109 . + . 10.g2
10 AUGUSTUS exon 23737 23937 . + . 10.g2
10 AUGUSTUS exon 25123 25203 . + . 10.g2

10 AUGUSTUS exon 27110 27939 . + . 10.g2
10 AUGUSTUS exon 50636 50833 . + . 10.g3
10 AUGUSTUS exon 59097 59219 . + . 10.g3
10 AUGUSTUS exon 60051 60138 . + . 10.g3
10 AUGUSTUS exon 61590 61689 . + . 10.g3

10 AUGUSTUS exon 62437 62607 . + . 10.g3
10 AUGUSTUS exon 74427 74832 . - . 10.g4
10 AUGUSTUS exon 77230 77312 . - . 10.g4
10 AUGUSTUS exon 80858 80963 . - . 10.g4
10 AUGUSTUS exon 81384 81449 . - . 10.g4
10 AUGUSTUS exon 84076 84284 . - . 10.g4
10 AUGUSTUS exon 86396 86603 . + . 10.g5
10 AUGUSTUS exon 92171 92326 . + . 10.g5
10 AUGUSTUS exon 97612 97801 . + . 10.g5
10 AUGUSTUS exon 102323 102795 . + . 10.g5
10 AUGUSTUS exon 104180 104288 . + . 10.g5
10 AUGUSTUS exon 107156 107309 . + . 10.g5
10 AUGUSTUS exon 107417 107547 . + . 10.g5
10 AUGUSTUS exon 112961 113096 . + . 10.g5
10 AUGUSTUS exon 113512 113866 . + . 10.g5
10 AUGUSTUS exon 115101 115548 . + . 10.g5


how can I do that?
Thanks in advance.
jamo
# 2  
Old 04-14-2013
You could try:
Code:
awk '
$NF != first {
        if(last) print last
        print 
        first = $NF
        last = "" 
        next
}       
{       last = $0}
END {   if(last) print last
}' input

As always, if you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg/bin/awk, or nawk instead of awk.

Oops! I misread the request, the above script prints the 1st and last lines of each set; not the lines between the 1st and last lines.

Last edited by Don Cragun; 04-14-2013 at 05:39 PM..
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-14-2013
Another one:
Code:
awk -F. 'p!=$NF{p=$NF; s=x; next} s x{print s} {s=$0}' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-14-2013
Quote:
Originally Posted by Scrutinizer
Another one:
Code:
awk -F. 'p!=$NF{p=$NF; s=x; next} s x{print s} {s=$0}' file

Hi Scrutinizer,
I usually like your code (although I sometimes find it a little bit terse) and don't see anything unneeded. But, I don't understand why you have:
Code:
s x{print s}

rather than
Code:
s{print s}

Could you explain what why you need to concatenate an empty string to s for this test?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-14-2013
Hi Don, this is to force s into a string context. I have found this to work reliably across awks.
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-14-2013
Quote:
Originally Posted by Scrutinizer
Hi Don, this is to force s into a string context. I have found this to work reliably across awks.
OK. Given that the last field contains an alphabetic character, I didn't see the need for forcing it to be treated as a string in this case, but it is safer if other data doesn't match what was shown in the example. (The only time this would matter is when the final field has the numeric value "0".)

I also noted that you treated "." as the field separator while I assumed the default field separator (spaces and tabs). The original specification isn't at all clear on this point. If it matters, jamo will have to clarify what is wanted.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 04-14-2013
Yes in this case it probably will not matter, but it is safer, and another potentially problematic situation ( probably not in this case ) would be a line that consists entirely of spacing (which would then not get printed)..
You are right about the dot FS. I assumed the OP meant the part after the last dot, but on rereading, it appears moer likely he meant the "10.g.." in which case the field seperator would need to be the default, and it would become:
Code:
awk 'p!=$NF{p=$NF; s=x; next} s x{print s} {s=$0}' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

3. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

4. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

5. Shell Programming and Scripting

Perl : to print the lines between two patterns

Hello experts, I have a text file from which I need to print all the lines between the patterns. Could anyone please help me with the perl script. names.txt ========= Badger Bald Eagle Bandicoot Bangle Tiger Barnacle Barracuda Basilisk Bass Basset Hound Beetle Beluga... (7 Replies)
Discussion started by: scriptscript
7 Replies

6. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

7. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

9. Shell Programming and Scripting

Print lines between two repetitive patterns

Hi users I have one file which has number of occurrence of one pattern examples Adjustmenttype,11 xyz 10 dwe 9 abd 13 def 14 Adjustmenttype,11 xyz 24 dwe 34 abd 35 def 11 nmb 12 Adjustmenttype, not eleven .... ... ... (2 Replies)
Discussion started by: eranmoh
2 Replies

10. Shell Programming and Scripting

print lines between two patterns in unix

Detroit Chicago Newyork Battlecreek Jackson Brooklyn How would I print only lines match between Detroit and Brooklyn used awk ? I don't want print Detroit and Brooklyn output should be : Chicago Newyork Battlecreek Jackson Thanks Jhonny (2 Replies)
Discussion started by: jhonnyrip
2 Replies
Login or Register to Ask a Question