Patrones en AWK



Una de las funciones más poderosas de AWK es la búsqueda de patrones en archivos de texto, como logs; posteriormente accionar sobre esas coincidencias nos abre posibilidades que no tendríamos en caso de hacerlo a golpe de teclazos.

La estructura es muy sencilla:

    awk '/expresion/ {acciones}' input

Bien, ahora que conocemos la estructura elemental, hagamos un ejercicio rápido.

awk script

#!/usr/bin/awk

/[0-9+]/ {print "Esta linea contiene digitos"}
/^$/ {print "Esta linea esta en blanco"}
/[a-zA-Z]+/ {print "Esta linea contiene caracteres"}


Entrada

AWK is a programming language designed for text processing and typically used as a data extraction and reporting tool. It is a standard feature of most Unix-like operating systems.

The AWK language is a data-driven scripting language consisting of a set of actions to be taken against streams of textual data – either run directly on files or used as part of a pipeline – for purposes of extracting or transforming text, such as producing formatted reports. The language extensively uses the string datatype, associative arrays (that is, arrays indexed by key strings), and regular expressions. While AWK has a limited intended application domain and was especially designed to support one-liner programs, the language is Turing-complete, and even the early Bell Labs users of AWK often wrote well-structured large AWK programs.[2]

AWK was created at Bell Labs in the 1970s,[3] and its name is derived from the surnames of its authors—Alfred Aho, Peter Weinberger, and Brian Kernighan. The acronym is pronounced the same as the name of the bird auk (which acts as an emblem of the language such as on The AWK Programming Language book cover[4] – the book is often referred to by the abbreviation TAPL). When written in all lowercase letters, as awk, it refers to the Unix or Plan 9 program that runs scripts written in the AWK programming language.


Salida


    $ awk -f matched_pattern.awk file_input.txt

Esta linea contiene caracteres
Esta linea esta en blanco
Esta linea contiene digitos
Esta linea contiene caracteres
Esta linea esta en blanco
Esta linea contiene digitos
Esta linea contiene caracteres

Comentarios

Entradas populares