Home > Mobile >  Print the lines between two lines that has the same pattern using bash
Print the lines between two lines that has the same pattern using bash

Time:04-09

I have a text file containing with string pattern 'OG' -

OG
ANNNFKSODJFHFJ
SSJSJKSKJSAJAS
SSSSSSSSSSSSFA
OG
FALJFNAFAFNAFJL
AFJLJSLJFLFSLFL
ASJFAJFAKFKAFKK
OG
AJSFLJASFLSFLFF
SJFLAFLAFLFLAFA
ASASFASFOFLJAJF

I want to print the lines between the first two lines that has 'OG' as string pattern, i.e. The result should be -

ANNNFKSODJFHFJ
SSJSJKSKJSAJAS
SSSSSSSSSSSSFA

Here, any suggestions using 'sed' or 'awk'. I don't want to use printing by line number but rather with pattern search.

I tried using awk, but not working -

awk '/^OG/{flag=1;next}/^OG/{flag=0}flag' file.txt

CodePudding user response:

You're almost there:

$ awk '/^OG/ {if(go) exit; go=1; next} go {print}' file.txt
ANNNFKSODJFHFJ
SSJSJKSKJSAJAS
SSSSSSSSSSSSFA
  • Related