Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save al-the-x/8697403 to your computer and use it in GitHub Desktop.

Select an option

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.
{
foo=1
bar=2
baz=3
}
{
foo=0
}
{
}
{
bar=1
baz=2
}
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 }
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