Pages

Tuesday, February 10, 2015

Print lines between patterns

Source: www.shellhacks.com/en/Using-SED-and-AWK-to-Print-Lines-Between-Two-Patterns+&cd=5&hl=en&ct=clnk&gl=us

File:

I Love Linux 
***** BEGIN ***** 
BASH is awesome 
BASH is awesome 
***** END ***** 
I Love Linux

  • sed 
Syntax :
sed -n '/StartPattern/,/EndPattern/p' FileName  <<<---inclusive both
Option Description
-n, --quiet, --silent Suppress automatic printing of pattern space
p Print the current pattern space
Example :
sed -n '/BEGIN/,/END/p' info.txt 
 
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****

  •  awk
Syntax :

awk '/StartPattern/,/EndPattern/' FileName

Example :

awk '/BEGIN/,/END/' info.txt

***** BEGIN *****
BASH is awesome
BASH is awesome
***** END ***** 


Another VERY Cool snipet:

source
http://stackoverflow.com/questions/9476018/split-text-file-into-parts-based-on-a-pattern-taken-from-the-text-file 


BEGIN { fn=0 }
NR==1 { next }
NR==2 { delim=$1 }
$1 == delim {
    f=sprintf("test%02d.txt",fn++);
    print "Creating " f
}

{ print $0 > f }

  1. initialize output file number
  2. ignore the first line
  3. extract the delimiter from the second line
  4. for every input line whose first token matches the delimiter, set up the output file name
  5. for all lines, write to the current output file

---------------------------------------- another ex:

* file: TEST_TAF PREF: RAC1 RAC2 RAC3 ...... AVAIL: RAC4  (PREF, AVAIL)
   >  sed 's/.*PREF//;s/AVAIL.*//' yourfile
   >  sed 's/.*PREF: //;s/ AVAIL.*//;s/  */,/g' yourfile

* https://nixtip.wordpress.com/2010/10/12/print-lines-between-two-patterns-the-awk-way/
file: 
test -3
test -2
test -1
OUTPUT
top 2
bottom 1
left 0
right 0
page 66
END
test 1
test 2
test 3
> awk '/OUTPUT/ {flag=1;next} /END/{flag=0} flag {print}'

-------------------------- Another Ex: starting with multiple patterns

sed -n '/\(12:05:43.376\|Begin FS_BP.Init.*Init.OnExecute\)/,/12:05:43.605/p'  AE_FS_BP_2740745.trc 

More examples ------ https://stackoverflow.com/questions/18185771/extract-nth-line-after-matching-pattern

To extract the Nth line after a matching pattern you want:
awk 'c&&!--c;/pattern/{c=N}' file
e.g.
awk 'c&&!--c;/Revision:/{c=5}' file
would print the 5th line after the text "Revision:"/.
FYI the following idioms describe how to select a range of records given a specific pattern to match:
a) Print all records from some pattern:
awk '/pattern/{f=1}f' file
b) Print all records after some pattern:
awk 'f;/pattern/{f=1}' file
c) Print the Nth record after some pattern:
awk 'c&&!--c;/pattern/{c=N}' file
d) Print every record except the Nth record after some pattern:
awk 'c&&!--c{next}/pattern/{c=N}1' file
e) Print the N records after some pattern:
awk 'c&&c--;/pattern/{c=N}' file
f) Print every record except the N records after some pattern:
awk 'c&&c--{next}/pattern/{c=N}1' file
g) Print the N records from some pattern:
awk '/pattern/{c=N}c&&c--' file
I changed the variable name from "f" for "found" to "c" for "count" where appropriate as that's more expressive of what the variable actually IS.