287 lines
3.2 KiB
Markdown
287 lines
3.2 KiB
Markdown
# awk
|
|
|
|
|
|
|
|
https://www.grymoire.com/Unix/Awk.html
|
|
|
|
|
|
|
|
```bash
|
|
$ cat test.txt
|
|
ubuntu Linux
|
|
mint Linux
|
|
debian Linux
|
|
raspbian Linux
|
|
mojave macOS
|
|
sierra macOS
|
|
tiger macOS
|
|
snowleopard macOS
|
|
```
|
|
|
|
|
|
|
|
### Colonnes:
|
|
|
|
Afficher la 1ere colonne d'un fichier:
|
|
|
|
```bash
|
|
$ awk '{print $1}' test.txt
|
|
ubuntu
|
|
mint
|
|
debian
|
|
raspbian
|
|
mojave
|
|
sierra
|
|
tiger
|
|
snowleopard
|
|
```
|
|
|
|
Afficher la 2eme colonne d'un fichier:
|
|
|
|
```bash
|
|
$ awk '{print $2}' test.txt
|
|
Linux
|
|
Linux
|
|
Linux
|
|
Linux
|
|
macOS
|
|
macOS
|
|
macOS
|
|
macOS
|
|
```
|
|
|
|
Afficher la 1ere colonne d'un fichier, excepté la 1ere ligne (header):
|
|
|
|
```bash
|
|
$ awk 'NR!=1{print $1}' test.txt
|
|
mint
|
|
debian
|
|
raspbian
|
|
mojave
|
|
sierra
|
|
tiger
|
|
snowleopard
|
|
|
|
# NR!=n° de ligne
|
|
```
|
|
|
|
Afficher le fichier entier:
|
|
|
|
```bash
|
|
$ awk '{print $0}' test.txt
|
|
ubuntu Linux
|
|
mint Linux
|
|
debian Linux
|
|
raspbian Linux
|
|
mojave macOS
|
|
sierra macOS
|
|
tiger macOS
|
|
snowleopard macOS
|
|
|
|
# commande identique:
|
|
$ awk '1' test.txt
|
|
```
|
|
|
|
|
|
|
|
Fichier avec séparateur:
|
|
|
|
```bash
|
|
$ cat test.csv
|
|
ubuntu,Linux
|
|
mint,Linux
|
|
debian,Linux
|
|
raspbian,Linux
|
|
mojave,macOS
|
|
sierra,macOS
|
|
tiger,macOS
|
|
snowleopard,macOS
|
|
```
|
|
|
|
Afficher la 1ere colonne d'un fichier:
|
|
|
|
il faut spécifier le séparateur:
|
|
|
|
```bash
|
|
$ awk -F"," '{print $1}' test.csv
|
|
ubuntu
|
|
mint
|
|
debian
|
|
raspbian
|
|
mojave
|
|
sierra
|
|
tiger
|
|
snowleopard
|
|
```
|
|
|
|
```bash
|
|
# idem, autre commande:
|
|
# FS (Field Separator)
|
|
$ awk '{print $2}' FS="," test.csv
|
|
Linux
|
|
Linux
|
|
Linux
|
|
Linux
|
|
macOS
|
|
macOS
|
|
macOS
|
|
macOS
|
|
```
|
|
|
|
Afficher les colonnes 1 et 2:
|
|
|
|
```bash
|
|
$ awk -F"," '{print $1, $2}' test.csv
|
|
ubuntu Linux
|
|
mint Linux
|
|
debian Linux
|
|
raspbian Linux
|
|
mojave macOS
|
|
sierra macOS
|
|
tiger macOS
|
|
snowleopard macOS
|
|
```
|
|
|
|
Afficher les colonnes 1 et 2, avec un séparateur:
|
|
|
|
```bash
|
|
# OFS (Output Field Separator)
|
|
|
|
$ awk -F"," '{print $1, $2}' OFS=";" test.csv
|
|
ubuntu;Linux
|
|
mint;Linux
|
|
debian;Linux
|
|
raspbian;Linux
|
|
mojave;macOS
|
|
sierra;macOS
|
|
tiger;macOS
|
|
snowleopard;macOS
|
|
```
|
|
|
|
Afficher la dernière colonne:
|
|
|
|
```bash
|
|
$ awk -F"," '{print $NF}' test.csv
|
|
Linux
|
|
Linux
|
|
Linux
|
|
Linux
|
|
macOS
|
|
macOS
|
|
macOS
|
|
macOS
|
|
```
|
|
|
|
Afficher l'avant-dernière colonne:
|
|
|
|
```bash
|
|
$ awk -F"," '{print $(NF - 1)}' test.csv
|
|
ubuntu
|
|
mint
|
|
debian
|
|
raspbian
|
|
mojave
|
|
sierra
|
|
tiger
|
|
snowleopard
|
|
```
|
|
|
|
Trim toute la colonne:
|
|
|
|
```bash
|
|
$ awk '{$1=$1;print}'
|
|
```
|
|
|
|
Supprimer la 1ere colonne:
|
|
|
|
```bash
|
|
$ awk '{$1=""; print $0}'
|
|
|
|
# les 3 premières colonnes
|
|
$ awk '{$1=$2=$3=""; print $0}'
|
|
```
|
|
|
|
Supprimer la dernière colonne:
|
|
|
|
```bash
|
|
$ awk -F"/" 'BEGIN{OFS=FS} {NF--; print}'
|
|
```
|
|
|
|
|
|
|
|
### Lignes:
|
|
|
|
Afficher la 3eme ligne:
|
|
|
|
```bash
|
|
$ awk 'NR==3' test.txt
|
|
mint
|
|
|
|
$ awk 'FNR == 3 {print}'
|
|
```
|
|
|
|
Afficher les ligne 2 à 4:
|
|
|
|
```bash
|
|
$ awk 'NR>=2 && NR<=4' test.txt
|
|
ubuntu
|
|
mint
|
|
debian
|
|
```
|
|
|
|
Afficher 4e colonne de la 2e ligne:
|
|
|
|
```bash
|
|
$ awk 'NR==2{print $4}'
|
|
```
|
|
|
|
Supprimer les lignes vides:
|
|
|
|
```bash
|
|
$ awk NF
|
|
```
|
|
|
|
Nombre de lignes:
|
|
|
|
```bash
|
|
$ awk 'END { print NR }' test.txt
|
|
```
|
|
|
|
Supprimer tout après la 1ere ligne vide:
|
|
|
|
```bash
|
|
$ awk '/^$/{exit} 1'
|
|
```
|
|
|
|
|
|
|
|
### Divers:
|
|
|
|
Passer un argument à awk:
|
|
|
|
```bash
|
|
$ x=3
|
|
|
|
$ awk '{print $0,val}' OFS=, val=$x test.csv
|
|
ubuntu,Linux,3
|
|
mint,Linux,3
|
|
debian,Linux,3
|
|
raspbian,Linux,3
|
|
mojave,macOS,3
|
|
sierra,macOS,3
|
|
tiger,macOS,3
|
|
snowleopard,macOS,3
|
|
|
|
$ export x
|
|
$ awk '{print $0,ENVIRON["x"]}' OFS=, test.csv
|
|
ubuntu,Linux,3
|
|
mint,Linux,3
|
|
debian,Linux,3
|
|
raspbian,Linux,3
|
|
mojave,macOS,3
|
|
sierra,macOS,3
|
|
tiger,macOS,3
|
|
snowleopard,macOS,3
|
|
```
|
|
|