From: Jack Shown on 6 Oct 2009 19:17 Is it possible using sed, awk, perl or anything else to search a file for a regex and if that line is followed by another regex then display both lines otherwise display only the first line? For example, suppose I grep a file for "Ultra" but want the following line also output if the following line contains "fixed". Thanks.
From: Ben Bacarisse on 6 Oct 2009 19:49 Jack Shown <jackshown(a)gmail.com> writes: > Is it possible using sed, awk, perl or anything else to search a file > for a regex and if that line is followed by another regex then display > both lines otherwise display only the first line? > > For example, suppose I grep a file for "Ultra" but want the following > line also output if the following line contains "fixed". Slightly clunky: grep -A1 Ultra <data | grep 'Ultra\|fixed' but it does what you ask and it will go wrong if either regex matches grep's group separator (--). -- Ben.
From: Janis Papanagnou on 6 Oct 2009 19:50 Jack Shown wrote: > Is it possible using sed, awk, perl or anything else to search a file > for a regex and if that line is followed by another regex then display > both lines otherwise display only the first line? awk ' /pattern1/ {print; flag=1; next} /pattern2/ && flag {print} {flag=0} ' Janis > > For example, suppose I grep a file for "Ultra" but want the following > line also output if the following line contains "fixed". > > Thanks.
From: sharma__r on 6 Oct 2009 20:20 On Oct 7, 4:17 am, Jack Shown <jacksh...(a)gmail.com> wrote: > Is it possible using sed, awk, perl or anything else to search a file > for a regex and if that line is followed by another regex then display > both lines otherwise display only the first line? > > For example, suppose I grep a file for "Ultra" but want the following > line also output if the following line contains "fixed". > > Thanks. One way to do it is (using "sed"): sed -e ' /Ultra/\!d n /fixed/\!d ' yourfile -- Rakesh
From: Grant on 6 Oct 2009 20:23
On Wed, 07 Oct 2009 01:50:16 +0200, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote: >Jack Shown wrote: >> Is it possible using sed, awk, perl or anything else to search a file >> for a regex and if that line is followed by another regex then display >> both lines otherwise display only the first line? > > awk ' > /pattern1/ {print; flag=1; next} > /pattern2/ && flag {print} > {flag=0} > ' Is it faster if you swapped /pattern2/ and flag? (does (g)awk do short circuit logic?) Grant. -- http://bugsplatter.id.au |