Linux Tutorial – Puntata 24
di jolek78
Care/i fruitrici e fruitori del blog, in questi post – ogni domenica mattina – si parlerà del sistema operativo GNU/Linux e degli strumenti per utilizzarlo al massimo delle potenzialità. Cercheremo di spiegare come funziona, cosa è il kernel, come costruirsi una vpn, come settare un firewall e giocare col terminale, usare adb e altro. Se il capitalismo della sorveglianza ci vuole passivi consumatori-consumatrici di servizi noi si risponde con un po’ di “cultura informatica” e un MARAMEO (quasi affettuoso). Buona lettura!
Ed eccoci qui,è domenica e, come da tradizione, poltrona, the caldo, e linux tutorial. Dopo aver elencato le espressioni regolari, e cominciato la scorsa puntata con find, questa puntata verra’ dedicata espressamente ad un altro tool ultra utilizzato su linux: grep. grep significa letteralmente “globally regular expression search and print”. Che tradotto per i non anglofoni significa “ricerca globale tramite espressioni regolari”. Che tradotto in non informatichese significa “strumento per cercare cose all’interno di altre cose per avere come risultato altre cose”. Chiaro no? No. Credetemi: la pratica e’ molto piu’ semplice. Spiegare l’informatica a parole e’ come spiegare la matematica senza utilizzare numeri. Si, si puo’ fare, ma la pratica vale molto piu’ che mille parole. E poiche’ un terminale linux ha sempre il suo fascino, come gli si puo’ resistere? Bentornati! Prima di tutto vediamo cosa ci comunicano le prime linee del manuale:
Man
NAME grep, egrep, fgrep, rgrep - print lines that match patterns SYNOPSIS grep [OPTION...] PATTERNS [FILE...] grep [OPTION...] -e PATTERNS ... [FILE...] grep [OPTION...] -f PATTERN_FILE ... [FILE...] DESCRIPTION grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input. In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.
Su linux , come su unix (lo abbiamo accennato diverse puntate fa) il sistema e’ case sensitive, e cio’ vuol dire che Pippo e’ diverso da pippo, pLUto e’ diverso da pLuto e paperiNo e’ diverso da paPerinO. Essendo grep un tool espressamente realizzato per estrarre informazioni, bisognera’ anche tenere conto di questo. Piccola notazione: anche in questa puntata utilizzeremo spesso il file contenente la novella di Coleridge.
Case Sensitive
Facendo una ricerca semplice, senza nessuna opzione, grep terra’ conto delle maiuscole o delle minuscole:
bottega@bottegadelbarbieri ~ grep "Mariner" the-rhyme-mariner.txt It is an ancient Mariner, The Mariner hath his will. The bright-eyed Mariner. The bright-eyed Mariner. 'God save thee, ancient Mariner! 'I fear thee, ancient Mariner! 'I fear thee, ancient Mariner!' When the Mariner's trance is abated.' The Mariner, whose eye is bright, bottega@bottegadelbarbieri ~ grep "mariner" the-rhyme-mariner.txt Came to the mariner's hollo! Came to the mariner's hollo! The mariners all 'gan work the ropes, He loves to talk with marineres
Case Insensitive (-i)
Inserendo invece l’opzione -i grep non ne terra’ conto delle maiuscole o delle minuscole. In altre parole e’ come se, invece che cercare “Mariner” cercasse [Mm][Aa][Rr][Ii][Nn][Ee][Rr]
bottega@bottegadelbarbieri ~ grep -i "Mariner" the-rhyme-mariner.txt It is an ancient Mariner, The Mariner hath his will. The bright-eyed Mariner. The bright-eyed Mariner. Came to the mariner's hollo! 'God save thee, ancient Mariner! Came to the mariner's hollo! 'I fear thee, ancient Mariner! The mariners all 'gan work the ropes, 'I fear thee, ancient Mariner!' When the Mariner's trance is abated.' He loves to talk with marineres The Mariner, whose eye is bright,
Occurrences (-c)
Per contare quante volte appare la ricerca che stiamo effettuando, bastera’ inserire l’opzione -c. E, ovviamente, lo faremo vedere nei tre modi:
bottega@bottegadelbarbieri ~ grep -c "Mariner" the-rhyme-mariner.txt 9 bottega@bottegadelbarbieri ~ grep -c "mariner" the-rhyme-mariner.txt 4 bottega@bottegadelbarbieri ~ grep -ic "mariner" the-rhyme-mariner.txt 13
Invert (-v)
Se invece proprio vogliamo eliminare qualcosa dall’output, basterà inserire l’opzione -v:
bottega@bottegadelbarbieri ~ grep -iv "mariner" the-rhyme-mariner.txt |head PART I And he stoppeth one of three. 'By thy long grey beard and glittering eye, Now wherefore stopp'st thou me? The Bridegroom's doors are opened wide, And I am next of kin; The guests are met, the feast is set: May'st hear the merry din.'
Search for filenames (-L || -l)
Se vogliamo invece non fare una ricerca nel contenuto dei file, ma soltanto del nome dei file. ci sono due modi. Il primo, eliminare dall’output la stringa cercata con l’opzione -L. Il secondo, includere nell’output la stringa cercata con l’opzione -l
bottega@bottegadelbarbieri ~ grep -li mariner /home/bottega/*.txt /home/bottega/the-rhyme-mariner-in.txt /home/bottega/the-rhyme-mariner.txt
bottega@bottegadelbarbieri ~ grep -Li mariner /home/bottega/*.txt /home/bottega/names.txt /home/bottega/test-err.txt /home/bottega/test.txt
Recursive search (-r)
Se invece, banalmente, vogliamo fare una ricerca ricorsiva in un particolare directory, si potrà utilizzare l’opzione -r:
bottega@bottegadelbarbieri ~ grep -ri mariner /home/bottega /home/bottega/test-out.txt:-rw-rw-r-- 1 bottega bottega 20272 May 16 03:02 the-rhyme-mariner.txt Binary file /home/bottega/.bash_history matches /home/bottega/the-rhyme-mariner-in.txt:It is an ancient Mariner, /home/bottega/the-rhyme-mariner-in.txt:The Mariner hath his will. /home/bottega/the-rhyme-mariner-in.txt:The bright-eyed Mariner. /home/bottega/the-rhyme-mariner-in.txt:The bright-eyed Mariner. /home/bottega/the-rhyme-mariner-in.txt:Came to the mariner's hollo! /home/bottega/the-rhyme-mariner-in.txt:'God save thee, ancient Mariner! /home/bottega/the-rhyme-mariner-in.txt:Came to the mariner's hollo! /home/bottega/the-rhyme-mariner-in.txt:'I fear thee, ancient Mariner! /home/bottega/the-rhyme-mariner-in.txt:The mariners all 'gan work the ropes, /home/bottega/the-rhyme-mariner-in.txt:'I fear thee, ancient Mariner!' /home/bottega/the-rhyme-mariner-in.txt:When the Mariner's trance is abated.' /home/bottega/the-rhyme-mariner-in.txt:He loves to talk with marineres /home/bottega/the-rhyme-mariner-in.txt:The Mariner, whose eye is bright, /home/bottega/the-rhyme-mariner.txt:It is an ancient Mariner, /home/bottega/the-rhyme-mariner.txt:The Mariner hath his will. /home/bottega/the-rhyme-mariner.txt:The bright-eyed Mariner. /home/bottega/the-rhyme-mariner.txt:The bright-eyed Mariner. /home/bottega/the-rhyme-mariner.txt:Came to the mariner's hollo! /home/bottega/the-rhyme-mariner.txt:'God save thee, ancient Mariner! /home/bottega/the-rhyme-mariner.txt:Came to the mariner's hollo! /home/bottega/the-rhyme-mariner.txt:'I fear thee, ancient Mariner! /home/bottega/the-rhyme-mariner.txt:The mariners all 'gan work the ropes, /home/bottega/the-rhyme-mariner.txt:'I fear thee, ancient Mariner!' /home/bottega/the-rhyme-mariner.txt:When the Mariner's trance is abated.' /home/bottega/the-rhyme-mariner.txt:He loves to talk with marineres /home/bottega/the-rhyme-mariner.txt:The Mariner, whose eye is bright,
Words (-w)
Facciamo finta ora che noi si voglia ricercare espressamente delle parole intere all’interno di un file. Non un pezzo di parola, un’espressione regolare, e un pezzo di parola. Proprio una parola. In questo caso si potra’ utilizzare l’opzione -w:
bottega@bottegadelbarbieri ~ grep -w mariner /home/bottega/the-rhyme-mariner.txt Came to the mariner's hollo! Came to the mariner's hollo! bottega@bottegadelbarbieri ~ grep -w Mariner /home/bottega/the-rhyme-mariner.txt It is an ancient Mariner, The Mariner hath his will. The bright-eyed Mariner. The bright-eyed Mariner. 'God save thee, ancient Mariner! 'I fear thee, ancient Mariner! 'I fear thee, ancient Mariner!' When the Mariner's trance is abated.' The Mariner, whose eye is bright,
Search for more than one word . (-E)
Un’opzione che ho scoperto soltanto 4 anni fa include la possibilita’ che grep ricerchi piu’ parole allo stesso momento. L’opzione si ottiene inserendo il parametro -E, e successivamente separando le parole dal simbolo |:
bottega@bottegadelbarbieri ~ grep -E "mariner|farewell" the-rhyme-mariner.txt Came to the mariner's hollo! Came to the mariner's hollo! The mariners all 'gan work the ropes, He loves to talk with marineres Farewell, farewell! but this I tell
bottega@bottegadelbarbieri ~ grep -iE "mariner|farewell" the-rhyme-mariner.txt It is an ancient Mariner, The Mariner hath his will. The bright-eyed Mariner. The bright-eyed Mariner. Came to the mariner's hollo! 'God save thee, ancient Mariner! Came to the mariner's hollo! 'I fear thee, ancient Mariner! The mariners all 'gan work the ropes, 'I fear thee, ancient Mariner!' When the Mariner's trance is abated.' He loves to talk with marineres Farewell, farewell! but this I tell The Mariner, whose eye is bright,
Restrict the output (-m)
Immaginate ora di avere un lungo output e di volerlo ridurre. Nel caso che presentiamo l’output e’ di 11 linee. Volendolo ridurre, basterà inserire l’opzione -m seguita dal numero di volte che vogliamo vedere la ricerca:
bottega@bottegadelbarbieri ~ grep -iw mariner the-rhyme-mariner.txt It is an ancient Mariner, The Mariner hath his will. The bright-eyed Mariner. The bright-eyed Mariner. Came to the mariner's hollo! 'God save thee, ancient Mariner! Came to the mariner's hollo! 'I fear thee, ancient Mariner! 'I fear thee, ancient Mariner!' When the Mariner's trance is abated.' The Mariner, whose eye is bright,
bottega@bottegadelbarbieri ~ grep -iwc mariner the-rhyme-mariner.txt 11 bottega@bottegadelbarbieri ~ grep -m5 -iw mariner the-rhyme-mariner.txt It is an ancient Mariner, The Mariner hath his will. The bright-eyed Mariner. The bright-eyed Mariner. Came to the mariner's hollo!
E le espressioni regolari dove sono? Qui di seguito. A voi pero’ il compito di capire cosa si sta cercando e in che modo. Come in una caccia al tesoro, 22 puo’ voler dire anche 0. O forse puntata?
– show file
bottega@bottegadelbarbieri ~ head the-rhyme-mariner.txt PART I It is an ancient Mariner, And he stoppeth one of three. 'By thy long grey beard and glittering eye, Now wherefore stopp'st thou me? The Bridegroom's doors are opened wide, And I am next of kin; The guests are met, the feast is set: May'st hear the merry din.'
– ^
bottega@bottegadelbarbieri ~ grep -m10 "^And" the-rhyme-mariner.txt And he stoppeth one of three. And I am next of kin; And listens like a three years' child: And thus spake on that ancient man, And he shone bright, and on the right And thus spake on that ancient man, And now the STORM-BLAST came, and he And chased us south along. And forward bends his head, And southward aye we fled.
– $
bottega@bottegadelbarbieri ~ grep -m10 "he$" the-rhyme-mariner.txt And now the STORM-BLAST came, and he The spirit slid: and it was he
– ^[..] || [..]$
bottega@bottegadelbarbieri ~ grep -m10 "^[A-Z]" the-rhyme-mariner.txt PART I It is an ancient Mariner, And he stoppeth one of three. Now wherefore stopp'st thou me? The Bridegroom's doors are opened wide, And I am next of kin; The guests are met, the feast is set: May'st hear the merry din.' He holds him with his skinny hand, Eftsoons his hand dropt he. bottega@bottegadelbarbieri ~ grep -m10 "[a-z]$" the-rhyme-mariner.txt Merrily did we drop And he shone bright, and on the right Nodding their heads before her goes And now the STORM-BLAST came, and he As who pursued with yell and blow And through the drifts the snowy clifts Why look'st thou so?'—With my cross-bow Still hid in mist, and on the left Nor any day for food or play For all averred, I had killed the bird
A voi continuare…
Alla prossima settimana!
jolek78
>> | Indice | << |
Puntata 23 | < > | Puntata 25 |
Il logo “Tux Linux” e’ stato realizzato e distribuito dall’artista deiby-ybied su Deviantart in licenza Creative Commons BY-NC-SA 3.0