15-03-2019
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
|
||||
https://ss64.com/vi.html
|
||||
|
||||
:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
@@ -40,6 +40,9 @@ then
|
||||
elif [ $foo -eq 2 ]
|
||||
then
|
||||
echo "Two"
|
||||
elif [ $foo -eq 3 ]
|
||||
then
|
||||
echo "Three"
|
||||
else
|
||||
echo "Bigger than two"
|
||||
fi
|
||||
@@ -63,6 +66,12 @@ then
|
||||
echo "Either you didn't give me a value or file is unreadable"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
|
||||
if [ $num -gt 100 ] && [ $num -lt 200 ]
|
||||
then
|
||||
echo "The number lies between 100 and 200"
|
||||
fi
|
||||
```
|
||||
|
||||
|
||||
@@ -243,7 +252,7 @@ if [ olderfile -ot newerfile ]; then
|
||||
if [ /mnt/remote/remotefile -ot localfile ]; then
|
||||
```
|
||||
|
||||
Si les fichiers <u>*same et file font référence au même device / inode*</u>
|
||||
Si les fichiers <u>*same et file font référence au même device / inode*</u>.
|
||||
|
||||
```bash
|
||||
if [ same -ef file ]; then
|
||||
@@ -251,7 +260,15 @@ if [ same -ef file ]; then
|
||||
if [ /dev/cdrom -ef /dev/dvd ]; then
|
||||
```
|
||||
|
||||
Si un fichier a été <u>modifié durant les 5 dernières minutes</u>.
|
||||
|
||||
```bash
|
||||
dir="/usr/local/etc/httpd"
|
||||
name="httpd.conf"
|
||||
|
||||
test=$(find $dir -name "$name" -mmin -5 -maxdepth 1)
|
||||
[ ! -z $test ] && echo -e "\033[1;31m❗️ ️$name was modified in the last 5 minutes\033[0m"
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -266,19 +283,19 @@ if [ “$1” == “moo” ]; then
|
||||
Si les chaines sont <u>*différentes*</u> [ STRING1 != STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$userinput” != “$password” ];
|
||||
if [ “$userinput” != “$password” ]; then
|
||||
```
|
||||
|
||||
Si la chaine 1 <u>*est triée après*</u> la chaine 2 [ STRING1 > STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$userinput” > “$password” ];
|
||||
if [ “$userinput” > “$password” ]; then
|
||||
```
|
||||
|
||||
Si la chaine 1 <u>*est triée avant*</u> la chaine 2 [ STRING1 < STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$userinput” < “$password” ];
|
||||
if [ “$userinput” < “$password” ]; then
|
||||
```
|
||||
|
||||
Si la chaine <u>*NONEMPTYSTRING a une longueur > 0*</u> (contient 1 ou plusieurs caractères)
|
||||
|
||||
14
docs/Linux/divers.md
Normal file
14
docs/Linux/divers.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Divers
|
||||
|
||||
|
||||
|
||||
#### Calculer la taille d'un dossier et des ses sous-dossiers:
|
||||
|
||||
```bash
|
||||
$ du -hcs folder/*
|
||||
8,0K folder/Camera
|
||||
8,0K total
|
||||
```
|
||||
|
||||
|
||||
|
||||
44
docs/Linux/format_ext4.md
Normal file
44
docs/Linux/format_ext4.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Formater un disque externe au format ext4
|
||||
|
||||
|
||||
|
||||
1. Ouvrir Gparted (Système->Administration dans Debian)
|
||||
2. Sélectionner le disque dans le menu déroulant à droite (/dev/sdg 698Gio)
|
||||
3. Cliquer sur la bande du disque
|
||||
4. Partition > Demonter
|
||||
5. Partition -> Supprimer -> Appliquer
|
||||
6. Tout l'espace est non alloué
|
||||
7. Partition -> Nouvelle
|
||||
Créer comme: Partiiton primaire
|
||||
Système de fichier: ext4
|
||||
Nouvelle taille: en Mio
|
||||
Ajouter une étiquette
|
||||
8. Ajouter -> Appliquer
|
||||
|
||||
|
||||
|
||||
Le disque est formaté ext4
|
||||
|
||||
Pour ce type de système de fichier (ext4, ext3...) les droits sont automatiquement donné à root (ce n'est pas le cas pour fat, ntfs...). L'utilisateur courant ne peut pas y écrire.
|
||||
|
||||
Il faut donc modifier les droits:
|
||||
|
||||
```bash
|
||||
bruno@debian:~$ cd /media/bruno/
|
||||
bruno@debian:/media/bruno$ ls -la
|
||||
total 12
|
||||
drwxr-x---+ 3 root root 4096 févr. 2 09:02 .
|
||||
drwxr-xr-x 4 root root 4096 janv. 22 20:50 ..
|
||||
drwxrwxr-x 5 root root 4096 févr. 2 08:15 RPi
|
||||
|
||||
bruno@debian:/media/bruno$ sudo chown bruno:bruno -R RPi/
|
||||
[sudo] Mot de passe de bruno :
|
||||
|
||||
bruno@debian:/media/bruno$ ls -la
|
||||
total 12
|
||||
drwxr-x---+ 3 root root 4096 févr. 2 09:02 .
|
||||
drwxr-xr-x 4 root root 4096 janv. 22 20:50 ..
|
||||
drwxrwxr-x 5 bruno bruno 4096 févr. 2 08:15 RPi
|
||||
|
||||
```
|
||||
|
||||
174
docs/Linux/read.md
Normal file
174
docs/Linux/read.md
Normal file
@@ -0,0 +1,174 @@
|
||||
# Lire un fichier
|
||||
|
||||
|
||||
|
||||
```bash
|
||||
$ cat test.txt
|
||||
ubuntu Linux
|
||||
mint Linux
|
||||
debian Linux
|
||||
raspbian Linux
|
||||
mojave macOS
|
||||
sierra macOS
|
||||
tiger macOS
|
||||
snowleopard macOS
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Lire un fichier:
|
||||
|
||||
```bash
|
||||
$ cat read.sh
|
||||
#!/bin/bash
|
||||
while read line
|
||||
do
|
||||
echo "Line is : $line"
|
||||
done < test.txt
|
||||
|
||||
$ ./read.sh
|
||||
Line is : ubuntu Linux
|
||||
Line is : mint Linux
|
||||
Line is : debian Linux
|
||||
Line is : raspbian Linux
|
||||
Line is : mojave macOS
|
||||
Line is : sierra macOS
|
||||
Line is : tiger macOS
|
||||
Line is : snowleopard macOS
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Lire un fichier tabulé:
|
||||
|
||||
```bash
|
||||
$ cat read.sh
|
||||
#!/bin/bash
|
||||
while read f1 f2
|
||||
do
|
||||
echo "Distrib : $f1"
|
||||
echo "OS : $f2"
|
||||
done < test.txt
|
||||
|
||||
$ ./read.sh
|
||||
Distrib : ubuntu
|
||||
OS : Linux
|
||||
Distrib : mint
|
||||
OS : Linux
|
||||
Distrib : debian
|
||||
OS : Linux
|
||||
Distrib : raspbian
|
||||
OS : Linux
|
||||
Distrib : mojave
|
||||
OS : macOS
|
||||
Distrib : sierra
|
||||
OS : macOS
|
||||
Distrib : tiger
|
||||
OS : macOS
|
||||
Distrib : snowleopard
|
||||
OS : macOS
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Changer le séparateur:
|
||||
|
||||
```bash
|
||||
$ cat read.sh
|
||||
#!/bin/bash
|
||||
while read f1 f2
|
||||
do
|
||||
echo $f1:$f2
|
||||
done < test.txt > test2.txt
|
||||
|
||||
$ cat test2.txt
|
||||
ubuntu:Linux
|
||||
mint:Linux
|
||||
debian:Linux
|
||||
raspbian:Linux
|
||||
mojave:macOS
|
||||
sierra:macOS
|
||||
tiger:macOS
|
||||
snowleopard:macOS
|
||||
```
|
||||
|
||||
|
||||
|
||||
Lire un fichier .csv:
|
||||
|
||||
```bash
|
||||
$ cat test.csv
|
||||
ubuntu,Linux
|
||||
mint,Linux
|
||||
debian,Linux
|
||||
raspbian,Linux
|
||||
mojave,macOS
|
||||
sierra,macOS
|
||||
tiger,macOS
|
||||
snowleopard,macOS
|
||||
|
||||
$ cat read.sh
|
||||
# !/bin/bash
|
||||
IFS=","
|
||||
while read f1 f2
|
||||
do
|
||||
echo "Distrib : $f1"
|
||||
echo "OS : $f2"
|
||||
done < test.csv
|
||||
|
||||
$ ./read.sh
|
||||
Distrib : ubuntu
|
||||
OS : Linux
|
||||
Distrib : mint
|
||||
OS : Linux
|
||||
Distrib : debian
|
||||
OS : Linux
|
||||
Distrib : raspbian
|
||||
OS : Linux
|
||||
Distrib : mojave
|
||||
OS : macOS
|
||||
Distrib : sierra
|
||||
OS : macOS
|
||||
Distrib : tiger
|
||||
OS : macOS
|
||||
Distrib : snowleopard
|
||||
OS : macOS
|
||||
```
|
||||
|
||||
|
||||
|
||||
Ne pas changer le séparateur définitivement:
|
||||
|
||||
```bash
|
||||
$ cat read.sh
|
||||
|
||||
# !/bin/bash
|
||||
OLDIFS=$IFS
|
||||
IFS=","
|
||||
while read f1 f2
|
||||
do
|
||||
echo "Distrib : $f1"
|
||||
echo "OS : $f2"
|
||||
done < test.csv
|
||||
IFS=$OLDIFS
|
||||
```
|
||||
|
||||
```bash
|
||||
$ cat read.sh
|
||||
|
||||
# !/bin/bash
|
||||
while IFS="," read f1 f2 f3
|
||||
do
|
||||
echo "Distrib : $f1"
|
||||
echo "OS : $f2"
|
||||
done < test.csv
|
||||
```
|
||||
|
||||
|
||||
|
||||
Multiples séparateurs:
|
||||
|
||||
```bash
|
||||
IFS=":/"
|
||||
```
|
||||
|
||||
@@ -18,11 +18,14 @@ funnymac@ssh funnymac@ftp.cluster011.ovh.net:/www/backup/
|
||||
Copie d'un fichier depuis le répertoire courant vers un répertoire du serveur:
|
||||
|
||||
```bash
|
||||
~ scp Fichier login@serveur:Chemin
|
||||
~ scp /home/mickael/data/Ficher2 root@192.168.10.131:/var/www/
|
||||
~ scp /Users/bruno/test-copy-scp-2.txt root@192.168.1.8:/root/
|
||||
$ scp Fichier login@serveur:Chemin
|
||||
$ scp /home/mickael/data/Ficher2 root@192.168.10.131:/var/www/
|
||||
$ scp /Users/bruno/test-copy-scp-2.txt root@192.168.1.8:/root/
|
||||
root@DiskStation:~ # scp /root/.profile funnymac@ftp.cluster011.ovh.net:www/backup/.profile_syno_root
|
||||
.profile 100% 497 0.5KB/s 0.5KB/s 00:00
|
||||
$ scp .bashrc pi@framboise.local:/home/pi/Desktop/
|
||||
.bashrc 100% 3640 401.7KB/s 00:00
|
||||
|
||||
```
|
||||
|
||||
Copie d'un répertoire, avec éventuellement ses sous-répertoires, vers un répertoire du serveur:
|
||||
|
||||
674
docs/Linux/sed.md
Normal file
674
docs/Linux/sed.md
Normal file
@@ -0,0 +1,674 @@
|
||||
# sed
|
||||
|
||||
|
||||
|
||||
```bash
|
||||
$ cat test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
#### Suppression de ligne:
|
||||
|
||||
Supprimer la <u>1ere ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed '1d' test.txt
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
|
||||
# le fichier test.txt n'est pas modifié
|
||||
```
|
||||
|
||||
```bash
|
||||
$ sed -i .bak '1d' test.txt
|
||||
$ cat test.txt
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
$ cat test.txt.bak
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
|
||||
# le fichier test.txt est sauvegardé en test.txt.bak
|
||||
# le fichier test.txt est modifié
|
||||
# Sinon pour masOS
|
||||
$ sed -i '' '1d' test.txt
|
||||
```
|
||||
|
||||
Supprimer la <u>3eme ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed '3d' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer la <u>dernière ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed '$d' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
```
|
||||
|
||||
Supprimer <u>de la 2eme à 4eme ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed '2,4d' test.txt
|
||||
red hat
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer les <u>lignes autres que de la 2eme à 4eme</u>:
|
||||
|
||||
```bash
|
||||
$ sed '2,4!d' test.txt
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
```
|
||||
|
||||
Supprimer la <u>1ere ET la dernière ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed '1d;$d' test.txt
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
```
|
||||
|
||||
Supprimer les lignes <u>commençant par le caractère 'u'</u>
|
||||
|
||||
```bash
|
||||
$ sed '/^u/d' test.txt
|
||||
red hat
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer les lignes se <u>terminant par le caractère 'n'</u>
|
||||
|
||||
```bash
|
||||
$ sed '/n$/d' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
```
|
||||
|
||||
Supprimer les lignes se <u>terminant par les caractères 'T', t', 'U' ou 'u'</u>
|
||||
|
||||
```bash
|
||||
$ sed '/[TtUu]$/d' test.txt
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer les lignes <u>vides</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/^$/d' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer les lignes <u>contenant l'expression 'red hat'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/red hat/d' test.txt
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer les lignes <u>ne contenant pas l'expression 'red hat'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/red hat/!d' test.txt
|
||||
red hat
|
||||
```
|
||||
|
||||
Supprimer les lignes <u>contenant les expressions 'ubuntu' OU 'mint'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/ubuntu\|mint/d' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
|
||||
# ne fonctionne pas sous macOS ???
|
||||
```
|
||||
|
||||
Supprimer de la <u>1ere ligne à la ligne contenant l'expression 'ubuntu'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '1,/ubuntu/d' test.txt
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer de la <u>ligne contenant l'expression 'ubuntu' jusqu'à la dernière ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/ubuntu/,$d' test.txt
|
||||
red hat
|
||||
```
|
||||
|
||||
Supprimer la <u>dernière ligne uniquement si elle contient l'expression 'raspbian'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '${/raspbian/d;}' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
```
|
||||
|
||||
Supprimer la <u>dernière ligne uniquement si elle contient les expressions 'raspbian' ou 'debian'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '${/raspbian\|debian/d;}' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
|
||||
# ne fonctionne pas sous macOS ???
|
||||
```
|
||||
|
||||
Supprime la ligne <u>contenant l'expression 'debian' uniquement. si elle est présente dans les lignes 1 à 4</u>:
|
||||
|
||||
```bash
|
||||
$ sed '1,4{/debian/d;}' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprime la <u>contenant l'expression 'mint' ET la ligne suivante</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/mint/{N;d;}' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprime la <u>ligne suivante celle contenant l'expression 'mint'</u>:
|
||||
|
||||
```bash
|
||||
$ sed '/mint/{N;s/\n.*//;}' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprime la <u>ligne contenant l'expression 'mint' ET la ligne précédente</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/mint/{s/.*//;x;d;};x;p;${x;p;}' test.txt | sed '/^$/d'
|
||||
red hat
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprime la <u>ligne précedent celle contenant l'expression 'mint'</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/mint/{x;d;};1h;1!{x;p;};${x;p;}' test.txt
|
||||
red hat
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprime la <u>ligne contenant l'expression 'mint', ainsi que la précédente et la suivante</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/mint/{N;s/.*//;x;d;};x;p;${x;p;}' test.txt | sed '/^$/d'
|
||||
red hat
|
||||
raspbian
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Affichage de ligne:
|
||||
|
||||
Afficher uniquement la <u>1ere ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '1p' test.txt
|
||||
red hat
|
||||
```
|
||||
|
||||
Afficher la <u>dernière ligne</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '$p' test.txt
|
||||
raspbian
|
||||
```
|
||||
|
||||
Afficher les lignes qui <u>ne contiennent pas 'b'</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/b/!p' test.txt
|
||||
red hat
|
||||
mint
|
||||
```
|
||||
|
||||
Afficher les lignes qui <u>contiennent 'h' ou 't'</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/[ht]/p' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
```
|
||||
|
||||
Afficher les lignes <u>se terminant par 'n' ou 'N'</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/[nN]$/p' test.txt
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Afficher les lignes qui <u>commencent par 'm' ou 'u'</u>:
|
||||
|
||||
```bash
|
||||
$ sed -n '/^m\|^u/p' test.txt
|
||||
# Ne marche pas sous macOS ???
|
||||
```
|
||||
|
||||
Afficher <u>1 ligne sur 2</u>:
|
||||
|
||||
```bash
|
||||
$ sed 'n;d' test.txt
|
||||
red hat
|
||||
mint
|
||||
raspbian
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Suprression de caractères:
|
||||
|
||||
Commande:
|
||||
|
||||
```bash
|
||||
$ sed 's/find/replace/' file
|
||||
```
|
||||
|
||||
Supprimer le caractère 'a'
|
||||
|
||||
```bash
|
||||
# supprimer le 1er caractère trouvé
|
||||
$ sed 's/a//' test.txt
|
||||
red ht
|
||||
ubuntu
|
||||
mint
|
||||
debin
|
||||
rspbian
|
||||
|
||||
# supprimer tous les caractères
|
||||
$ sed 's/a//g' test.txt
|
||||
red ht
|
||||
ubuntu
|
||||
mint
|
||||
debin
|
||||
rspbin
|
||||
```
|
||||
|
||||
Supprimer le 1er caractère de chaque ligne:
|
||||
|
||||
```bash
|
||||
$ sed 's/^.//' test.txt
|
||||
ed hat
|
||||
buntu
|
||||
int
|
||||
ebian
|
||||
aspbian
|
||||
|
||||
# autre commande identique
|
||||
$ sed 's/.//' test.txt
|
||||
```
|
||||
|
||||
Supprimer le dernier caractère de chaque ligne:
|
||||
|
||||
```bash
|
||||
$ sed 's/.$//' test.txt
|
||||
red ha
|
||||
ubunt
|
||||
min
|
||||
debia
|
||||
raspbia
|
||||
```
|
||||
|
||||
Supprimer les 1er et dernier caractères de chaque ligne:
|
||||
|
||||
```bash
|
||||
$ sed 's/.//;s/.$//' test.txt
|
||||
ed ha
|
||||
bunt
|
||||
in
|
||||
ebia
|
||||
aspbia
|
||||
```
|
||||
|
||||
Supprimer le 1er caractère de chaque ligne, mais seulement celui spécifié (ici le 'u'):
|
||||
|
||||
```bash
|
||||
$ sed 's/^u//' test.txt
|
||||
red hat
|
||||
buntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer le dernier caractère de chaque ligne, mais seulement celui spécifié (ici le 'u'):
|
||||
|
||||
```bash
|
||||
$ sed 's/n$//' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debia
|
||||
raspbia
|
||||
```
|
||||
|
||||
Supprimer les 3 premiers caractères de chaque ligne:
|
||||
|
||||
```bash
|
||||
$ sed 's/...//' test.txt
|
||||
hat
|
||||
ntu
|
||||
t
|
||||
ian
|
||||
pbian
|
||||
```
|
||||
|
||||
Supprimer les n premiers caractères de chaque ligne (ici 3):
|
||||
|
||||
```bash
|
||||
$ sed -r 's/.{3}//' test.txt
|
||||
|
||||
# ne fonctionne pas sous macOS
|
||||
```
|
||||
|
||||
Supprimer les n derniers caractères de chaque ligne (ici 3):
|
||||
|
||||
```bash
|
||||
$ sed -r 's/.{3}$//'
|
||||
|
||||
# ne fonctionne pas sous macOS
|
||||
```
|
||||
|
||||
Tout supprimer sauf les n premiers caractères de chaque ligne (ici 3):
|
||||
|
||||
```bash
|
||||
$ sed -r 's/(.{3}).*/\1/' test.txt
|
||||
|
||||
# ne fonctionne pas sous macOS
|
||||
```
|
||||
|
||||
Tout supprimer sauf les n derniers caractères de chaque ligne (ici 3):
|
||||
|
||||
```bash
|
||||
$ sed -r 's/.*(.{3})/\1/' test.txt
|
||||
|
||||
# ne fonctionne pas sous macOS
|
||||
```
|
||||
|
||||
Supprimer plusieurs caractères (ici toutes les voyelles):
|
||||
|
||||
```bash
|
||||
$ sed 's/[aeiou]//g' test.txt
|
||||
rd ht
|
||||
bnt
|
||||
mnt
|
||||
dbn
|
||||
rspbn
|
||||
```
|
||||
|
||||
Supprimer une expression ('bia'):
|
||||
|
||||
```bash
|
||||
$ sed 's/bia//g' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
den
|
||||
raspn
|
||||
```
|
||||
|
||||
Supprimer seulement la n ième occurence d'un caractère:
|
||||
|
||||
```bash
|
||||
$ sed 's/u//2' test.txt
|
||||
red hat
|
||||
ubntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Tout supprimer après le caractère spécifié ('b'):
|
||||
|
||||
```bash
|
||||
$ sed 's/b.*//' test.txt
|
||||
red hat
|
||||
u
|
||||
mint
|
||||
de
|
||||
rasp
|
||||
```
|
||||
|
||||
Supprimer tous les nombres:
|
||||
|
||||
```bash
|
||||
$ sed 's/[0-9]//g' test.txt
|
||||
```
|
||||
|
||||
Supprimer toutes les minuscules:
|
||||
|
||||
```bash
|
||||
$ sed 's/[a-z]//g' test.txt
|
||||
|
||||
|
||||
|
||||
|
||||
$
|
||||
# supprime les caractères mais laisse des lignes vides
|
||||
```
|
||||
|
||||
Supprimer tout sauf les minuscules:
|
||||
|
||||
```bash
|
||||
$ sed 's/[^a-z]//g' test.txt
|
||||
redhat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Supprimer tous les caractères alpha-numériques:
|
||||
|
||||
```bash
|
||||
$ sed 's/[a-zA-Z0-9]//g' test.txt
|
||||
|
||||
|
||||
|
||||
|
||||
$
|
||||
# supprime les caractères mais laisse des lignes vides
|
||||
```
|
||||
|
||||
Supprimer minuscule et majuscule d'un caractère:
|
||||
|
||||
```bash
|
||||
$ sed 's/[uU]//g' test.txt
|
||||
red hat
|
||||
bnt
|
||||
mint
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Remplacement de caractères:
|
||||
|
||||
Ajouter une expression en début de ligne:
|
||||
|
||||
```bash
|
||||
$ sed 's/^/Distrib: /' test.txt
|
||||
Distrib: red hat
|
||||
Distrib: ubuntu
|
||||
Distrib: mint
|
||||
Distrib: debian
|
||||
Distrib: raspbian
|
||||
```
|
||||
|
||||
Ajouter une expression en fin de ligne:
|
||||
|
||||
```bash
|
||||
$ sed 's/$/ Tux ruleeeeez/' test.txt
|
||||
red hat Tux ruleeeeez
|
||||
ubuntu Tux ruleeeeez
|
||||
mint Tux ruleeeeez
|
||||
debian Tux ruleeeeez
|
||||
raspbian Tux ruleeeeez
|
||||
```
|
||||
|
||||
Substituer un caractère (le 1er trouvé de chaque ligne):
|
||||
|
||||
```bash
|
||||
$ sed 's/a/A/' test.txt
|
||||
red hAt
|
||||
ubuntu
|
||||
mint
|
||||
debiAn
|
||||
rAspbian
|
||||
```
|
||||
|
||||
Substituer tous les caractères:
|
||||
|
||||
```bash
|
||||
$ sed 's/a/A/g' test.txt
|
||||
red hAt
|
||||
ubuntu
|
||||
mint
|
||||
debiAn
|
||||
rAspbiAn
|
||||
```
|
||||
|
||||
Subsituer uniquement la 2nde occurence:
|
||||
|
||||
```bash
|
||||
$ sed 's/a/A/2' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
mint
|
||||
debian
|
||||
raspbiAn
|
||||
```
|
||||
|
||||
Subsituer toutes les occurences à partir de la 2nde:
|
||||
|
||||
```bash
|
||||
$ sed 's/u/U/2g' test.txt
|
||||
|
||||
# ne fonctionne pas sous macOS
|
||||
```
|
||||
|
||||
Substituer uniquement sur le n eme ligne (3eme):
|
||||
|
||||
```bash
|
||||
$ sed '3s/n/N/g' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
miNt
|
||||
debian
|
||||
raspbian
|
||||
```
|
||||
|
||||
Subtituer uniquement de la 3e à la 4e ligne:
|
||||
|
||||
```bash
|
||||
$ sed '3,4s/n/N/g' test.txt
|
||||
red hat
|
||||
ubuntu
|
||||
miNt
|
||||
debiaN
|
||||
raspbian
|
||||
```
|
||||
|
||||
Remplacer la ligne entière:
|
||||
|
||||
```bash
|
||||
$ sed 's/.*/& is a distrib Linux/' test.txt
|
||||
red hat is a distrib Linux
|
||||
ubuntu is a distrib Linux
|
||||
mint is a distrib Linux
|
||||
debian is a distrib Linux
|
||||
raspbian is a distrib Linux
|
||||
```
|
||||
|
||||
Multiples substitutions:
|
||||
|
||||
```bash
|
||||
$ sed 's/a/A/g; s/u/uUu/g' test.txt
|
||||
red hAt
|
||||
uUubuUuntuUu
|
||||
mint
|
||||
debiAn
|
||||
rAspbiAn
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Ajouter une ligne (à voir pour macOS):
|
||||
|
||||
Ajouter une ligne d'entête:
|
||||
|
||||
```bash
|
||||
$ sed '1i Employee, EmpId' test.txt
|
||||
# GNU sed, ne marche pas pour macOS
|
||||
```
|
||||
|
||||
Ajouter '————————' après la ligne d'entête:
|
||||
|
||||
```bash
|
||||
$ sed -i '1a ---------------' test.txt
|
||||
```
|
||||
|
||||
Ajouter une ligne tout à la fin:
|
||||
|
||||
```bash
|
||||
$ sed -i '$a ---------------' test.txt
|
||||
```
|
||||
77
docs/Linux/string.md
Normal file
77
docs/Linux/string.md
Normal file
@@ -0,0 +1,77 @@
|
||||
# String
|
||||
|
||||
|
||||
|
||||
Supprimer la 1ere ligne d'un fichier:
|
||||
|
||||
```bash
|
||||
$ npm ls | sed '1,1d'
|
||||
```
|
||||
|
||||
Supprimer les 2 premières lignes d'un fichier:
|
||||
|
||||
```bash
|
||||
$ npm ls | sed '1,2d'
|
||||
```
|
||||
|
||||
|
||||
|
||||
Supprimer les lignes contenant un motif:
|
||||
|
||||
```bash
|
||||
$ npm ls | grep -v 'motif'
|
||||
```
|
||||
|
||||
|
||||
|
||||
Récupérer la 1ere sous-chaine d'une chaine:
|
||||
|
||||
```bash
|
||||
$ string='924782627 GPX Editor (2.96.10)'
|
||||
|
||||
$ echo "$string" | awk '{print $1}'
|
||||
924782627
|
||||
```
|
||||
|
||||
|
||||
|
||||
Récupérer tout sauf la 1ere sous-chaine d'une chaine:
|
||||
|
||||
```bash
|
||||
$ string='924782627 GPX Editor (2.96.10)'
|
||||
|
||||
$ echo "$string" | awk {'first = $1; $1=""; print $0'}
|
||||
GPX Editor (2.96.10)
|
||||
|
||||
# Supprimer le 1er espace
|
||||
$ echo "$string" | awk {'first = $1; $1=""; print $0'}| sed 's/^ //g'
|
||||
GPX Editor (2.96.10)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
awk:
|
||||
|
||||
```bash
|
||||
$ string='924782627 GPX Editor (2.96.10)'
|
||||
|
||||
$ echo "$string"
|
||||
924782627 GPX Editor (2.96.10)
|
||||
# Par défaut le séparateur de awk est l'espace
|
||||
$ echo "$string" | awk '{print $1}'
|
||||
924782627
|
||||
$ echo "$string" | awk '{print $2}'
|
||||
GPX
|
||||
$ echo "$string" | awk '{print $4}'
|
||||
(2.96.10)
|
||||
# On met '(' comme séparateur
|
||||
$ echo "$string" | awk -F "(" '{print $1}'
|
||||
924782627 GPX Editor
|
||||
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user