Last active
August 29, 2015 13:55
-
-
Save al-the-x/8697403 to your computer and use it in GitHub Desktop.
AWK script to find number of occurrences of "foo=" in a very large text file (or stream); supplied is a small example of the format one might expect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| foo=1 | |
| bar=2 | |
| baz=3 | |
| } | |
| { | |
| foo=0 | |
| } | |
| { | |
| } | |
| { | |
| bar=1 | |
| baz=2 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BEGIN { has_foo = 0 } | |
| { | |
| if ( !has_foo && /foo=/ ) has_foo = 1 | |
| if ( /^\}/ ) { | |
| if ( has_foo ) n_foos++ | |
| has_foo = 0 | |
| } | |
| } | |
| END { print n_foos + 0 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| while not eof | |
| read a line | |
| if not current_block_has_foo | |
| if line matches /^\s*foo=/ | |
| current_block_has_foo = true | |
| if line is "}" | |
| if current_block_has_foo | |
| blocks_with_foo += 1 | |
| current_block_has_foo = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment