Top Banner
Almir 'm3nd3s' @m3nd3s [email protected] http://www.almirmendes.com/ http://www.flickr.com/photos/atomicshed/2405111237/ awk
32

Palestra sobre AWK [Giran Siege]

Jan 18, 2015

Download

Technology

Palestra no Giran Siege sobre a linguagem de programação AWK para processamento de textos.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Palestra sobre AWK [Giran Siege]

Almir 'm3nd3s'@[email protected]://www.almirmendes.com/

http://www.flickr.com/photos/atomicshed/2405111237/

awk

Page 2: Palestra sobre AWK [Giran Siege]

O que é o AWK?

Page 3: Palestra sobre AWK [Giran Siege]

AWK é uma linguagem de programação

para processamento de dados baseados em texto

Page 5: Palestra sobre AWK [Giran Siege]

$ man awk

Page 6: Palestra sobre AWK [Giran Siege]

Quando usar

http://www.flickr.com/photos/nuagedenuit/155699074/

Page 7: Palestra sobre AWK [Giran Siege]

Quando temos um conteúdo em formato texto particularmente

separado por delimitador

Page 8: Palestra sobre AWK [Giran Siege]

Quando um comando ou uma sequência Quando um comando ou uma sequência de comandos do shell (cut, grep, etc.) de comandos do shell (cut, grep, etc.)

não “parecem” suficientes para obter o não “parecem” suficientes para obter o resultado desejadoresultado desejado

http://www.flickr.com/photos/anotherjesse/151464940/

Page 9: Palestra sobre AWK [Giran Siege]

Quando utilizar linguagens como Quando utilizar linguagens como Java, C, Pascal, etc., parece Java, C, Pascal, etc., parece

“apelação”“apelação”

http://www.flickr.com/photos/linssen/5226407901/

Page 10: Palestra sobre AWK [Giran Siege]

Quando os dados “tem cara de AWK”

Page 11: Palestra sobre AWK [Giran Siege]

awk [pattern] { action }

Page 12: Palestra sobre AWK [Giran Siege]

awk { print $0 } /etc/passwd

cat /etc/passwd | awk { print $0 }

Page 13: Palestra sobre AWK [Giran Siege]

Os $x são os campos

O $0 é toda linha

O NF é..... ?

Page 14: Palestra sobre AWK [Giran Siege]

Delimitador default é o espaço

Mas você pode trocar!Basta usar o -F

Page 15: Palestra sobre AWK [Giran Siege]

Isto não me parece uma linguagem de programação?!

¬¬'

Page 16: Palestra sobre AWK [Giran Siege]

Controle de fluxo

if, while, do while, for, for in, continue, break

Page 17: Palestra sobre AWK [Giran Siege]

echo "10:34" | awk -F: '{ if($2<30) printf("%d:00",$1)

else printf("%d:30",$1)}'

Page 18: Palestra sobre AWK [Giran Siege]

Tem suporte a Regex?

expr ~ /er/

expr !~ /er/

Page 19: Palestra sobre AWK [Giran Siege]

date | awk '$1 ~ /^Ter/

{ print "Siege Day - " $0 }'

Page 20: Palestra sobre AWK [Giran Siege]

As expressões são As expressões são similaressimilares ao da ao da

linguagem Clinguagem C

http://www.flickr.com/photos/mrbill/2482009942/

Page 21: Palestra sobre AWK [Giran Siege]

Assignment = += -= *= /= %= ^=conditional ? :logical or ||logical and &&array membership inmatching ~ !~relational < > <= >= == !=concatenation (no explicit operator)add ops + -mul ops * / %unary + -logical not !exponentiation ^inc and dec ++ -- (both post and pre)field $

Page 22: Palestra sobre AWK [Giran Siege]

Variáveis pré-definidas

FILENAMENFNR

OFSORS

Page 23: Palestra sobre AWK [Giran Siege]

Funções internas de Funções internas de stringstring

http://www.flickr.com/photos/61417318@N00/4908148942/

Page 24: Palestra sobre AWK [Giran Siege]

Você também pode definir funções!

Page 25: Palestra sobre AWK [Giran Siege]

function csplit(s, A, n, i) { n = length(s) for( i = 1 ; i <= n ; i++ )A[i] = substr(s, i, 1) return n}

Page 26: Palestra sobre AWK [Giran Siege]

Kramba.. isso na linha de Kramba.. isso na linha de comando é osso!comando é osso!

http://www.flickr.com/photos/bittersweet_memories/4388392704/

Page 27: Palestra sobre AWK [Giran Siege]

awk -f prog.awk arquivo

Page 28: Palestra sobre AWK [Giran Siege]

Exemplos!!!!!!!!!!!!!O__o

Page 29: Palestra sobre AWK [Giran Siege]

Emulando o cat:

{ print }

Page 30: Palestra sobre AWK [Giran Siege]

Emulando o wc:

{ chars += length($0) + 1; words += NF }

END { print NR, words, chars }

Page 31: Palestra sobre AWK [Giran Siege]

Referências

$ man awkhttp://pt.wikipedia.org/wiki/Awk

google.com

Page 32: Palestra sobre AWK [Giran Siege]

Almir 'm3nd3s'@m3nd3s

[email protected]://www.almirmendes.net/