From: Ed Morton on 8 Oct 2009 14:41 sharma__r(a)hotmail.com wrote: > On Oct 8, 12:28 am, Ed Morton <mortons...(a)gmail.com> wrote: >> On Oct 7, 1:57 pm, sharma...(a)hotmail.com wrote: >> <snip> >> >>> Thanks for pointing that out. Actually it just needs a minor change: >>> sed -e ' >>> :top >>> /pattern1/!d >>> n >>> /pattern2/b >>> btop >>> ' yourfile >>> -- Rakesh- Hide quoted text - >> Just curious, if you don't mind indulging me: how would you enhance >> that script if your requirements changed slightly to, say, print the >> line numbers before the lines, i.e. the equivalent of changing this: >> >> awk '/Ultra/ {print; found=1; next} >> found && /fixed/ {print} >> {found=0}' file >> >> to this: >> >> awk '/Ultra/ {print NR,$0; found=1; next} >> found && /fixed/ {print NR,$0} >> {found=0}' file >> >> or if you wanted to only print the second line if it was longer than >> 10 characters: >> >> awk '/Ultra/ {print; found=1; next} >> found && /fixed/ && (length>10) {print} >> {found=0}' file >> >> or if you wanted to, at the end of running the script, print a count >> of all the lines that matched each pattern: >> >> awk '/Ultra/ {print; found=1; count1++; next} >> found && /fixed/ {print; count2++} >> {found=0} >> END{print count1+0, count2+0}' file >> >> Regards, >> >> Ed. > > > You could try this on a bourne-shell based command line: > > ## to print the line numbers alongwith the lines > sed -e ' > :a > /RE1/!d > =;n > /RE2/!ba > = > ' yourfile > > > ## to print the second line only if it's length were more than 10 > characters. > sed -e ' > :a > /RE1/!d > n > /RE2/!ba > /.\{10\}/!d > ' yourfile > > > -- Rakesh Thanks! Ed. |