Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print lines if 1line has pattern and immediately followed by next ptrn 1

Status
Not open for further replies.

spike4000

IS-IT--Management
Nov 6, 2003
2
US
I have a file like this:
a10a
b20b
c50c
d10d
e30e
f10f
g20g

I want to print only the lines that have 10 in them and are immmediately followed by a line containing 20. My output for this should be:
a10a
b20b
f10f
g20g
Can you help?
Thanks.
 
Code:
/10/  { first = $0 ; next }
first && /20/ { print first; print }
first = ""

Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.
 
small performance update of futurelet's code

Code:
/10/ { first = $0; getline; if (/20/) print first "\n"  $0 }

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Awk automatically reads the lines of the file. It is unwise to take on that responsibility by using getline unless you really have to. When you become familiar with Awk, you will lose the tendency to use getline at the drop of a hat.

And your "performance update" is actually slower than my code.

The gawk manual states:
"The getline command is used in several different ways and should not be used by beginners ... come back and study the getline command after you have reviewed the rest of this Web page and have a good knowledge of how awk works."
 
I would agree halfways about using getline where its neccessary, but i didnt know the snippet from the gawk manual. Now i nearly totally would :)

About the performance, i posted it blindy.. benchmarks show it ~40% faster with gawk and 100% faster with mawk (where mawk is around 3x faster than gawk in general), on linux.

I wouldnt mind see'ing a post of proov that its slower (cat /dev/urandom | strings | head -240000).. im more darwin/linux than any commercial unix

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Timings on file of size 4,199,958, using "The One True AWK" by Brian Kernighan (the "K" in AWK):

[tt]
mine yours
------- --------
1.6 sec 2.65 sec
[/tt]
I timed each program 4 times using Ultra Precision Command Timer 1.6 and calculated the average. Test was done in a dos-box under windoze.

Your code is good, but I think one should usually let AWK read the files without interference.
 
Ai ai ai, i dunno.. please let me mark those timings as highly exceptional for diverse reasons =p

(nm my english, if you come to the same case on unix, lemme know, or not.. which awk did you use)

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top