Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
04-linux:22-awk [2016/11/16 11:36] – Roge | 04-linux:22-awk [2017/10/06 23:38] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== awk ====== | ||
+ | |||
+ | ===== Références ===== | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | AWK est un langage interprété de traitement de données sophistiqué. | ||
+ | Plus évolué que grep, sed ou autres. | ||
+ | |||
+ | Le principe est d' | ||
+ | |||
+ | //"The awk utility reads the input files one line at a time. For each line, awk tries the patterns of each of the rules. If several patterns match, then several actions are run in the order in which they appear in the awk program. If no patterns match, then no actions are run. | ||
+ | |||
+ | After processing all the rules that match the line (and perhaps there are none), awk reads the next line. (However, see Next Statement, and also see Nextfile Statement). This continues until the program reaches the end of the file." [Effective AWK Programming]// | ||
+ | |||
+ | ===== Données de référence ===== | ||
+ | |||
+ | |||
+ | Les données de référence pour les exemples : | ||
+ | < | ||
+ | $ cat addressbook.txt | ||
+ | Amelia | ||
+ | Anthony | ||
+ | Becky 555-7685 | ||
+ | Bill | ||
+ | Broderick | ||
+ | Camilla | ||
+ | Fabius | ||
+ | Julie 555-6699 | ||
+ | Martin | ||
+ | Samuel | ||
+ | Jean-Paul | ||
+ | </ | ||
+ | |||
+ | ===== Exemple 1 ===== | ||
+ | |||
+ | Sélection de lignes. | ||
+ | |||
+ | Le programme awk : | ||
+ | <code bash> | ||
+ | # | ||
+ | BEGIN { | ||
+ | | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | /thon/ { print } | ||
+ | /li/ { print } | ||
+ | |||
+ | END { | ||
+ | print " | ||
+ | </ | ||
+ | |||
+ | L' | ||
+ | < | ||
+ | $ ./test2.awk addressbook.txt | ||
+ | ------------- BEGIN AWK Test 2 ------------- | ||
+ | Amelia | ||
+ | Anthony | ||
+ | Broderick | ||
+ | Julie 555-6699 | ||
+ | Samuel | ||
+ | ------------- END AWK Test 2 ------------- | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Exemple 2 ===== | ||
+ | |||
+ | Substitution de caractères puis sélection de lignes. | ||
+ | |||
+ | Le programme awk : | ||
+ | <code bash> | ||
+ | # | ||
+ | /lia/ { gsub(/lia/, " | ||
+ | /thon/ { print } | ||
+ | /li/ { print } | ||
+ | </ | ||
+ | |||
+ | L' | ||
+ | < | ||
+ | $ ./test3.awk addressbook.txt | ||
+ | Anthony | ||
+ | Broderick | ||
+ | Julie 555-6699 | ||
+ | Samuel | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Exemple 3 ===== | ||
+ | |||
+ | Inversion du code de téléphone. | ||
+ | |||
+ | Le programme awk : | ||
+ | <code bash> | ||
+ | # | ||
+ | /li/ { print gensub(/ | ||
+ | </ | ||
+ | |||
+ | Variante avec changement global : | ||
+ | |||
+ | <code bash> | ||
+ | # | ||
+ | { $0=gensub(/ | ||
+ | /li/ { print } | ||
+ | </ | ||
+ | |||
+ | L' | ||
+ | <code bash> | ||
+ | $ ./test3.awk addressbook.txt | ||
+ | Amelia | ||
+ | Broderick | ||
+ | Julie 6699-555 | ||
+ | Samuel | ||
+ | </ | ||
+ | |||
+ | <note tip> Seul '' | ||
+ | </ | ||
+ | |||
+ | |||
+ | <note tip> A la différence de sub et gsub, '' | ||
+ | </ | ||
+ | |||
+ | ===== Exemple script complexe ===== | ||
+ | |||
+ | <file bash log2html.awk> | ||
+ | # | ||
+ | BEGIN { | ||
+ | print "< | ||
+ | print "< | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | # print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | |||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " tr th {" | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " th, td {" | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print "</ | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | # convert Cygwin path to Dos path | ||
+ | function convert2DosPath() { | ||
+ | | ||
+ | } | ||
+ | |||
+ | function setErrorLine() { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | function emphasiseLine() { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | function createTitle() { | ||
+ | | ||
+ | while (match($0, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | while (match($0, | ||
+ | | ||
+ | while (match($0, | ||
+ | if (diskSizeCheck) createDfReportTable() | ||
+ | } | ||
+ | |||
+ | function createDfReportTable() { | ||
+ | | ||
+ | print "< | ||
+ | print "< | ||
+ | while (! match($0, | ||
+ | if ($5 > 90) { | ||
+ | print "< | ||
+ | } else if ($5 > 80) { | ||
+ | print "< | ||
+ | } else if ($5 > 50) { | ||
+ | print "< | ||
+ | } else { | ||
+ | print "< | ||
+ | } | ||
+ | print "< | ||
+ | getline | ||
+ | } | ||
+ | print "</ | ||
+ | } | ||
+ | |||
+ | function createCompareReportTable() { | ||
+ | | ||
+ | # Afficher les erreurs éventuelles | ||
+ | while (! match($0, " | ||
+ | if (! match($0, | ||
+ | | ||
+ | |||
+ | } | ||
+ | getline | ||
+ | } | ||
+ | | ||
+ | | ||
+ | print "< | ||
+ | print "< | ||
+ | while (! match($0, " | ||
+ | parseReportLine() | ||
+ | } | ||
+ | | ||
+ | | ||
+ | print "</ | ||
+ | | ||
+ | } | ||
+ | |||
+ | function setTd(val) { | ||
+ | | ||
+ | } | ||
+ | function setTdDifferent(val) { | ||
+ | | ||
+ | } | ||
+ | function setTdRight(val) { | ||
+ | | ||
+ | } | ||
+ | function setTdRightDifferent(val) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | function parseReportLine() { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | if (dir1 == "" | ||
+ | $0=gensub(/ | ||
+ | items2=gensub(/ | ||
+ | size2=gensub(/ | ||
+ | } else if (dir2 == "" | ||
+ | $0=gensub(/< | ||
+ | items1=gensub(/ | ||
+ | size1=gensub(/ | ||
+ | } else { | ||
+ | $0=gensub(/ | ||
+ | items1=gensub(/ | ||
+ | size1=gensub(/ | ||
+ | items2=gensub(/ | ||
+ | size2=gensub(/ | ||
+ | } | ||
+ | |||
+ | if ( dir1 != dir2 ) { | ||
+ | lleft=setTdDifferent(dir1) | ||
+ | lrigth=setTdDifferent(dir2) | ||
+ | } else { | ||
+ | lleft=setTd(dir1) | ||
+ | lrigth=setTd(dir2) | ||
+ | } | ||
+ | if ( items1 != items2 ) { | ||
+ | lleft = lleft setTdRightDifferent(items1) | ||
+ | lrigth = lrigth setTdRightDifferent(items2) | ||
+ | } else { | ||
+ | lleft = lleft setTdRight(items1) | ||
+ | lrigth = lrigth setTdRight(items2) | ||
+ | } | ||
+ | if ( size1 != size2 ) { | ||
+ | lleft = lleft setTdRightDifferent(size1) | ||
+ | lrigth = lrigth setTdRightDifferent(size2) | ||
+ | } else { | ||
+ | lleft = lleft setTdRight(size1) | ||
+ | lrigth = lrigth setTdRight(size2) | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | #remove all empty lines | ||
+ | $0 ~ EMPTY_LINE | ||
+ | |||
+ | # Time and duration formatting | ||
+ | $0 ~ " " DATE { gsub(DATE, "< | ||
+ | $0 ~ TIME { gsub(TIME, "< | ||
+ | $0 ~ DURATION { gsub(DURATION, | ||
+ | |||
+ | # Path patch | ||
+ | $0 ~ CYGDRIVE { $0=convert2DosPath() } | ||
+ | |||
+ | # Special lines formatting | ||
+ | / - kept/ { $0="< | ||
+ | / - deleted|^deleting / { $0="< | ||
+ | /^rsync |log: |^compareDirs|ile created: / { emphasiseLine() } | ||
+ | |||
+ | /^rsync error/ { setErrorLine() } | ||
+ | /^Error / { setErrorLine() } | ||
+ | |||
+ | /<span class=" | ||
+ | |||
+ | /.*: Fin de / { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | $0 ~ LINE____ {createTitle()} | ||
+ | |||
+ | { print } | ||
+ | |||
+ | # Print html file end | ||
+ | END { print "</ | ||
+ | </ | ||