#! /usr/bin/awk -f # ## Demonstrate the usage of pattern range matching in awk scripts. # # This code also shows how to open a standard input file in the BEGIN block so # that it does not need to be specified on the command line. # ## PD 2004 Manfred Waßmann http://www.berlinos.de/ ## # # Copyright (C) 2004 Manfred Waßmann http://www.berlinos.de/ # # This file is public domain. # BEGIN{ #temp file used as input file tmpf="rangepat.tmp"; #create input file print"eins\nzwei\ndrei\nvier\nsechs\nacht\nzehn\nelf\nvierzehn\n"\ "zwanzig\ndreissig\nvierzig\nhundert"\ >tmpf; close(tmpf); print" *** INPUT *** "; system("cat "tmpf) print" *** OUTPUT *** "; #make awk use tmpf as input file ARGV[ARGC++]=tmpf; #define pattern variables begpat="^z"; endpat="^v"; } #save text matched by begpat in head; print saved text in out, iff. $1~begpat{head=$1;if(out){print out;out=""}} #process input file regions that start with begpat and end with endpat. $1~begpat,$1~endpat{ #if head is set this is the first line of the region if(head){ print" ======= "head" ======= "; head=""; }else{ if(out){print out;} out=$0; } } END{if(out){print out}}