unito differenti repository
This commit is contained in:
331
lpic/101/012_elaborare_flussi_di_testo.md
Normal file
331
lpic/101/012_elaborare_flussi_di_testo.md
Normal file
@ -0,0 +1,331 @@
|
||||
# Reindirizzamenti e pipe
|
||||
|
||||
Tutti i programmi di manipolazione del testo partono da un input standard (`stdin`), lo inviano a un output standard (`stdout`) e inviano eventuali errori a un output degli errori standard (`stderr`). Solitamente, `stdin` e' quello digitato da tastiera.
|
||||
|
||||
Il carattere `>` dice a `cat` di indirizzare il suo output sul file `newfile`, non sullo `stdout`:
|
||||
|
||||
```bash
|
||||
cat > newfile
|
||||
Nuovo file di prova
|
||||
^C
|
||||
|
||||
cat newfile
|
||||
Nuovo file di prova
|
||||
```
|
||||
E' possibile usare la `|` per indirizzare l'output di un programma ad un altro programma.
|
||||
|
||||
### Ottenere una parte di un file di testo
|
||||
|
||||
Il comando `head` e' usato per leggere di default le prime 10 righe di un file, mentre il comando `tail` le ultime 10. Per indicare il numero di righe e' possibile reindirizzare l'output al comando `nl`:
|
||||
|
||||
```bash
|
||||
head /var/log/alternatives.log | nl
|
||||
1 update-alternatives 2024-10-02 20:46:38: run with --install /usr/bin/pager pager /bin/more 50 --slave /usr/share/man/man1/pager.1.gz pager.1.gz /usr/share/man/man1/more.1.gz
|
||||
2 update-alternatives 2024-10-02 20:47:08: run with --install /usr/bin/x-www-browser x-www-browser /usr/bin/brave-browser-nightly 1
|
||||
3 update-alternatives 2024-10-02 20:47:08: run with --install /usr/bin/gnome-www-browser gnome-www-browser /usr/bin/brave-browser-nightly 1
|
||||
4 update-alternatives 2024-10-02 20:47:08: run with --install /usr/bin/brave-browser brave-browser /usr/bin/brave-browser-nightly 1
|
||||
```
|
||||
|
||||
Oppure, per contare il numero di righe totali di un file:
|
||||
|
||||
```bash
|
||||
cat logs/borg_backup.log | wc -l
|
||||
10153
|
||||
```
|
||||
|
||||
## sed
|
||||
|
||||
Possiamo usare `sed` per cercare solo le righe che contengono un determinato pattern:
|
||||
|
||||
```bash
|
||||
sed -n /cat/p < ftu.txt
|
||||
|
||||
cat
|
||||
zcat
|
||||
bzcat
|
||||
xzcat
|
||||
```
|
||||
|
||||
il simbolo `<` indirizza il contenuto del file al comando `sed`. La stringa racchiusa dagli *slash*, ovvero `/cat/`, e' la stringa che stiamo ricercando. L'opzione `-n` indica a sed di non produrre output, se non quello generato dal comando `p`.
|
||||
|
||||
```bash
|
||||
sed /cat/d < ftu.txt
|
||||
cut
|
||||
head
|
||||
ls
|
||||
man
|
||||
md5sum
|
||||
nl
|
||||
paste
|
||||
sed
|
||||
sort
|
||||
split
|
||||
sort
|
||||
vim
|
||||
cd
|
||||
tail
|
||||
uniq
|
||||
wc
|
||||
mv
|
||||
cp
|
||||
sed
|
||||
passwd
|
||||
pwd
|
||||
```
|
||||
In questo modo `sed` visualizzera' tutto il contenuto del file, eccetto cio' che `d` indica a *sed* di cancellare dal suo output.
|
||||
|
||||
Un uso comune di *sed* e' quello di *trovare e sostituire del testo* all'interno di un file. Per sostituire tutte le occorrenze di *cat* con *dog*:
|
||||
|
||||
```bash
|
||||
sed s/cat/dog/ < ftu.txt
|
||||
|
||||
cut
|
||||
head
|
||||
ls
|
||||
man
|
||||
md5sum
|
||||
nl
|
||||
paste
|
||||
sed
|
||||
sort
|
||||
dog
|
||||
zdog
|
||||
split
|
||||
sort
|
||||
vim
|
||||
cd
|
||||
tail
|
||||
uniq
|
||||
wc
|
||||
mv
|
||||
cp
|
||||
sed
|
||||
passwd
|
||||
pwd
|
||||
bzdog
|
||||
xzdog
|
||||
```
|
||||
|
||||
E' possibile passare direttamente il file a `sed`:
|
||||
|
||||
```bash
|
||||
sed s/cat/dog/ ftu.txt
|
||||
cut
|
||||
head
|
||||
ls
|
||||
man
|
||||
md5sum
|
||||
nl
|
||||
paste
|
||||
sed
|
||||
sort
|
||||
dog
|
||||
zdog
|
||||
split
|
||||
sort
|
||||
vim
|
||||
cd
|
||||
tail
|
||||
uniq
|
||||
wc
|
||||
mv
|
||||
cp
|
||||
sed
|
||||
passwd
|
||||
pwd
|
||||
bzdog
|
||||
xzdog
|
||||
```
|
||||
|
||||
In realta', il file non e' stato mai modificato. Per procedere, creando una copia di backup:
|
||||
|
||||
```bash
|
||||
sed -i.backup s/cat/dog/ ftu.txt
|
||||
|
||||
ll ftu*
|
||||
Permissions Size User Group Date Modified Name
|
||||
.rw-rw-r-- 114 dado dado 29 ott 21:19 ftu.txt
|
||||
.rw-rw-r-- 114 dado dado 29 ott 21:09 ftu.txt.backup
|
||||
```
|
||||
|
||||
In questo modo, viene creato un file di backup e modificato direttamente il file originale.
|
||||
|
||||
## Integrita' dei dati
|
||||
|
||||
- md5sum
|
||||
- sha256sum
|
||||
- sha512sum
|
||||
|
||||
Per calcolare il valore SHA256 del file *ftu.txt* dare il seguente comando:
|
||||
|
||||
```bash
|
||||
sha256sum ftu.txt
|
||||
94cc7c49dfe55cd824d4e0c2f88881fba9daae41c11601f08b1bf818cd77a671 ftu.txt
|
||||
```
|
||||
|
||||
Se creiamo un file che contiene il valore di checksum, possiamo utilizzarlo per verificare l'integrita' del file:
|
||||
|
||||
```bash
|
||||
sha256sum ftu.txt > sha256sum.txt
|
||||
|
||||
sha256sum -c sha256sum.txt
|
||||
ftu.txt: OK
|
||||
```
|
||||
|
||||
Se il file originale fosse modificato, il controllo fallirebbe:
|
||||
|
||||
```bash
|
||||
echo "new line" >> ftu.txt
|
||||
|
||||
sha256sum -c sha256sum.txt
|
||||
ftu.txt: NON RIUSCITO
|
||||
sha256sum: WARNING: 1 computed checksum did NOT match
|
||||
```
|
||||
|
||||
### Esercizi
|
||||
|
||||
```bash
|
||||
root:x:0:0:root:/root:/bin/bash
|
||||
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
|
||||
bin:x:2:2:bin:/bin:/usr/sbin/nologin
|
||||
sys:x:3:3:sys:/dev:/usr/sbin/nologin
|
||||
sync:x:4:65534:sync:/bin:/bin/sync
|
||||
nvidia-persistenced:x:121:128:NVIDIA Persistence Daemon,,,:/nonexistent:/sbin/nologin
|
||||
libvirt-qemu:x:64055:130:Libvirt Qemu,,,:/var/lib/libvirt:/usr/sbin/nologin
|
||||
libvirt-dnsmasq:x:122:133:Libvirt Dnsmasq,,,:/var/lib/libvirt/dnsmasq:/usr/sbin/nologin
|
||||
carol:x:1000:2000:Carol Smith,Finance,,,Main Office:/home/carol:/bin/bash
|
||||
dave:x:1001:1000:Dave Edwards,Finance,,,Main Office:/home/dave:/bin/ksh
|
||||
emma:x:1002:1000:Emma Jones,Finance,,,Main Office:/home/emma:/bin/bash
|
||||
frank:x:1003:1000:Frank Cassidy,Finance,,,Main Office:/home/frank:/bin/bash
|
||||
grace:x:1004:1000:Grace Kearns,Engineering,,,Main Office:/home/grace:/bin/ksh
|
||||
henry:x:1005:1000:Henry Adams,Sales,,,Main Office:/home/henry:/bin/bash
|
||||
john:x:1006:1000:John Chapel,Sales,,,Main Office:/home/john:/bin/bash
|
||||
```
|
||||
|
||||
- Elenca solo gli utenti del gruppo 1000
|
||||
|
||||
```bash
|
||||
[20:21 sab nov 02]dado@pc (957):~
|
||||
> grep 1000 passwd
|
||||
carol:x:1000:2000:Carol Smith,Finance,,,Main Office:/home/carol:/bin/bash
|
||||
dave:x:1001:1000:Dave Edwards,Finance,,,Main Office:/home/dave:/bin/ksh
|
||||
emma:x:1002:1000:Emma Jones,Finance,,,Main Office:/home/emma:/bin/bash
|
||||
frank:x:1003:1000:Frank Cassidy,Finance,,,Main Office:/home/frank:/bin/bash
|
||||
grace:x:1004:1000:Grace Kearns,Engineering,,,Main Office:/home/grace:/bin/ksh
|
||||
henry:x:1005:1000:Henry Adams,Sales,,,Main Office:/home/henry:/bin/bash
|
||||
john:x:1006:1000:John Chapel,Sales,,,Main Office:/home/john:/bin/bash
|
||||
|
||||
[20:21 sab nov 02]dado@pc (958):~
|
||||
> sed -n /1000/p < passwd
|
||||
carol:x:1000:2000:Carol Smith,Finance,,,Main Office:/home/carol:/bin/bash
|
||||
dave:x:1001:1000:Dave Edwards,Finance,,,Main Office:/home/dave:/bin/ksh
|
||||
emma:x:1002:1000:Emma Jones,Finance,,,Main Office:/home/emma:/bin/bash
|
||||
frank:x:1003:1000:Frank Cassidy,Finance,,,Main Office:/home/frank:/bin/bash
|
||||
grace:x:1004:1000:Grace Kearns,Engineering,,,Main Office:/home/grace:/bin/ksh
|
||||
henry:x:1005:1000:Henry Adams,Sales,,,Main Office:/home/henry:/bin/bash
|
||||
john:x:1006:1000:John Chapel,Sales,,,Main Office:/home/john:/bin/bash
|
||||
```
|
||||
|
||||
- Elenca solo i nomi completi di questi utenti
|
||||
|
||||
```bash
|
||||
[20:23 sab nov 02]dado@pc (962):~
|
||||
> sed -n /1000/p < passwd | cut -d : -f 5 | cut -d , -f 1
|
||||
Carol Smith
|
||||
Dave Edwards
|
||||
Emma Jones
|
||||
Frank Cassidy
|
||||
Grace Kearns
|
||||
Henry Adams
|
||||
John Chapel
|
||||
```
|
||||
|
||||
- Un comando che seleziona un utente a caso del Main Office
|
||||
|
||||
```bash
|
||||
[20:26 sab nov 02]dado@pc (971):~
|
||||
> sed -n /Main\ Office/p < passwd | cut -d : -f 5 | cut -d , -f 1 | sort -R | head -n 1
|
||||
```
|
||||
|
||||
- Quante persone lavorano in Finance, Engineering e Sales?
|
||||
|
||||
```bash
|
||||
[20:28 sab nov 02]dado@pc (978):~
|
||||
> grep -iE "finance|engineering|sales" passwd | wc -l
|
||||
7
|
||||
|
||||
[20:28 sab nov 02]dado@pc (979):~
|
||||
> grep -iE "finance" passwd | wc -l
|
||||
4
|
||||
|
||||
[20:30 sab nov 02]dado@pc (981):~
|
||||
> grep -iE "engineering" passwd | wc -l
|
||||
1
|
||||
|
||||
[20:30 sab nov 02]dado@pc (982):~
|
||||
> grep -iE "sales" passwd | wc -l
|
||||
2
|
||||
|
||||
[21:09 sab nov 02]dado@pc (1108):~
|
||||
> sed -n /Main\ Office/p < passwd | cut -d , -f 2 | sort | uniq -c
|
||||
1 Engineering
|
||||
4 Finance
|
||||
2 Sales
|
||||
```
|
||||
|
||||
- Creare un file .csv col formato seguente
|
||||
|
||||
```bash
|
||||
First Name, Last Name, Position
|
||||
|
||||
|
||||
[20:40 sab nov 02]dado@pc (1016):~
|
||||
> sed -n /Main\ Office/p < passwd | cut -d : -f 5 | cut -d , -f 1,2 | cut -d " " -f 1,2 --output-delimiter=, > names.csv
|
||||
Carol,Smith,Finance
|
||||
Dave,Edwards,Finance
|
||||
Emma,Jones,Finance
|
||||
Frank,Cassidy,Finance
|
||||
Grace,Kearns,Engineering
|
||||
Henry,Adams,Sales
|
||||
John,Chapel,Sales
|
||||
```
|
||||
|
||||
- Assicurasi l'integrita' del file precedente tramite `md5sum`:
|
||||
|
||||
```bash
|
||||
[20:44 sab nov 02]dado@pc (1024):~
|
||||
> md5sum names.csv > md5sum_names.txt
|
||||
|
||||
[20:45 sab nov 02]dado@pc (1026):~
|
||||
> md5sum -c md5sum_names.txt
|
||||
names.csv: OK
|
||||
|
||||
[20:45 sab nov 02]dado@pc (1027):~
|
||||
> echo "new line" >> md5sum_names.txt
|
||||
|
||||
[20:46 sab nov 02]dado@pc (1028):~
|
||||
> md5sum -c md5sum_names.txt
|
||||
names.csv: OK
|
||||
md5sum: WARNING: 1 line is improperly formatted
|
||||
```
|
||||
|
||||
- Dall'output di `ls -l /etc`, ottenere solo i nomi dei file
|
||||
|
||||
```bash
|
||||
[21:13 sab nov 02]dado@pc (1114):~
|
||||
> ls -l /etc/ | tr -s " " | cut -d " " -f 9
|
||||
```
|
||||
|
||||
- Nome e proprietario
|
||||
|
||||
```bash
|
||||
[21:13 sab nov 02]dado@pc (1114):~
|
||||
> ls -l /etc/ | tr -s " " | cut -d " " -f 9,3
|
||||
```
|
||||
|
||||
- SOLO i nomi delle cartelle e del proprietario
|
||||
|
||||
```bash
|
||||
[21:16 sab nov 02]dado@pc (1120):~
|
||||
> ls -l /etc/ | tr -s " " | grep ^d | cut -d " " -f 9,3
|
||||
```
|
Reference in New Issue
Block a user