From: Janis Papanagnou on 6 Oct 2009 20:42 Grant wrote: > 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? Probably; if you are processing many millions of lines. :-) > (does (g)awk do short circuit logic?) Yes. Janis > > Grant.
From: Ed Morton on 6 Oct 2009 23:27 sharma__r(a)hotmail.com wrote: > 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 Hmm, that fails for me: $ sed -e ' /Ultra/\!d n /fixed/\!d ' file sed: -e expression #1, char 12: unknown command: `\' $ sed --version GNU sed version 4.1.5 What Janis suggested seems to work, though: $ awk '/Ultra/ {print; found=1; next} found && /fixed/ {print} {found=0}' file Regards, Ed.
From: Jack Shown on 6 Oct 2009 23:48 On Oct 6, 4:50 pm, Janis Papanagnou <janis_papanag...(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} > ' > > Janis > > > > > > > For example, suppose I grep a file for "Ultra" but want the following > > line also output if the following line contains "fixed". > > > Thanks. Works perfectly! Thanks, Janis and everyone else.
From: pk on 7 Oct 2009 04:31 Ed Morton wrote: >> One way to do it is (using "sed"): >> >> sed -e ' >> /Ultra/\!d >> n >> /fixed/\!d >> ' yourfile > > Hmm, that fails for me: > > $ sed -e ' > /Ultra/\!d > n > /fixed/\!d > ' file > sed: -e expression #1, char 12: unknown command: `\' Yeah you don't need the backslash before the "!" (or use awk as you already said).
From: sharma__r on 7 Oct 2009 11:47 On Oct 7, 8:27 am, Ed Morton <mortons...(a)gmail.com> wrote: > sharma...(a)hotmail.com wrote: > > 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 > > Hmm, that fails for me: > > $ sed -e ' > /Ultra/\!d > n > /fixed/\!d > ' file > sed: -e expression #1, char 12: unknown command: `\' > > $ sed --version > GNU sed version 4.1.5 > This will work: sed -e ' /pattern1/!d n /pattern2/!d ' yourfile -- Rakesh
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: A couple of sed questions Next: How to ls -lh on a FreeBSD? |