1

How do I print from the nth match to the mth match in a file?

For example

>1
dfgsdrhsdtrh
>2
zdfrgsdh
>3
zdsfgadrh
>4
sdtrh
>5
drh

If I want the 2nd to the 4th match to the regexp '^>', including the Nth match and up to but not including the M+1th match. Yielding, for example:

>2
zdfrgsdh
>3
zdsfgadrh
>4
sdtrh
stuppie
  • 384
  • 1
  • 8
  • 18

3 Answers3

1

Using gnu-awk you can use this awk:

awk -v RS='>' -v ORS='>' 'BEGIN{printf ">"} NR>5{exit} NR>2 && NR<=5' file
>2
zdfrgsdh
>3
zdsfgadrh
>4
sdtrh
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You could use the \K construct if supported. If not, just remove the \K and put capture
parenthesis around what comes after it.

 #  (?m)(?:^>.*\r?\n(?:^(?!>).*\r?\n)*){1}\K(?:^>.*\r?\n(?:^(?!>).*\r?\n)*){3}

 (?m)                    # MULTI_LINE mode

 (?:                     # 0 to N-1
      ^ > .* \r? \n 
      (?:
           ^ 
           (?! > )
           .* \r? \n 
      )*
 ){1}                    # Do N-1 times

 \K                      # Disgard previous from match

 (?:                     # N to M+1
      ^ > .* \r? \n 
      (?:
           ^ 
           (?! > )
           .* \r? \n 
      )*
 ){3}                    # Do M+1-N times

Output:

 **  Grp 0 -  ( pos 18 , len 40 ) 
>2
zdfrgsdh
>3
zdsfgadrh
>4
sdtrh
0

Here is an awk solution:

awk -v RS=">" '$1>=2 && $1<=4 {$0=RS$0;gsub(/\n$/,"");print}' file
>2
zdfrgsdh
>3
zdsfgadrh
>4
sdtrh

It prints record from >2 to >4

Jotne
  • 40,548
  • 12
  • 51
  • 55