03-04-2020
This commit is contained in:
150
docs/Divers/bash/strings.md
Normal file
150
docs/Divers/bash/strings.md
Normal file
@@ -0,0 +1,150 @@
|
||||
# Chaine de caractères:
|
||||
|
||||
|
||||
|
||||
##### Concaténer une chaine:
|
||||
|
||||
```bash
|
||||
upd+="$name "
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Trim une chaine:
|
||||
|
||||
1. sed
|
||||
|
||||
```bash
|
||||
$ var=" Une chaine entourée d'espaces "
|
||||
|
||||
$ echo "Fluctuat nec $var mergitur"
|
||||
Fluctuat nec Une chaine entourée d'espaces mergitur
|
||||
|
||||
# supprimer les espaces en fin de chaine: sed 's/ *$//g'`
|
||||
$ var=`echo $var | sed 's/ *$//g'`
|
||||
|
||||
$ echo "Fluctuat nec $var mergitur"
|
||||
Fluctuat nec Une chaine entourée d'espaces mergitur
|
||||
|
||||
# supprimer les espaces en début de chaine: sed 's/^ *//g'`
|
||||
$ var=`echo $var | sed 's/^ *//g'`
|
||||
|
||||
$ echo "Fluctuat nec $var mergitur"
|
||||
Fluctuat nec Une chaine entourée d'espaces mergitur
|
||||
|
||||
# supprimer les espaces en début et fin de chaine:
|
||||
|
||||
$ var=" Une chaine entourée d'espaces "
|
||||
$ echo "$var" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//'
|
||||
Une chaine entourée d'espaces
|
||||
```
|
||||
|
||||
2. awk
|
||||
|
||||
```bash
|
||||
$ echo "${var}"
|
||||
Une chaine entourée d'espaces
|
||||
|
||||
$ echo "${var}" | awk '{gsub(/^[ \t]+/,""); print $0, " fin" }'
|
||||
Une chaine entourée d'espaces fin
|
||||
|
||||
$ echo "${var}" | awk '{gsub(/[ \t]+$/,""); print $0, " fin" }'
|
||||
Une chaine entourée d'espaces fin
|
||||
|
||||
$ echo "${var}" | awk '{gsub(/^[ \t]+| [ \t]+$/,""); print $0, " fin" }'
|
||||
Une chaine entourée d'espaces fin
|
||||
```
|
||||
|
||||
3. xargs
|
||||
|
||||
```bash
|
||||
$ var=" Une chaine entourée d espaces "
|
||||
|
||||
$ echo "$var" | xargs
|
||||
Une chaine entourée d espaces
|
||||
|
||||
$ echo " Une chaine entourée d espaces " | xargs
|
||||
Une chaine entourée d espaces
|
||||
|
||||
# erreur si la chaine contient un apostrophe
|
||||
$ echo " Une chaine entourée d'espaces " | xargs
|
||||
xargs: unterminated quote
|
||||
|
||||
# dans ce cas, on utilise xargs -0
|
||||
$ echo " Une chaine entourée d'espaces " | xargs -0
|
||||
Une chaine entourée d'espaces
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Longueur d'une chaine:
|
||||
|
||||
```bash
|
||||
$ echo -n "Longueur de la chaine:" | wc -c
|
||||
22
|
||||
|
||||
$ echo "Longueur de la chaine:" | awk '{print length}'
|
||||
22
|
||||
|
||||
$ var="Longueur de la chaine:"
|
||||
$ echo ${#var}
|
||||
22
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Sous-chaine:
|
||||
|
||||
```bash
|
||||
$ string=abcABC123ABCabc
|
||||
|
||||
$ echo ${string:0}
|
||||
abcABC123ABCabc
|
||||
|
||||
$ echo ${string:1}
|
||||
bcABC123ABCabc
|
||||
|
||||
$ echo ${string:7}
|
||||
23ABCabc
|
||||
|
||||
$ echo ${string:7:3}
|
||||
23A
|
||||
|
||||
$ echo ${stringZ: -4} # Notez l'espace
|
||||
Cabc
|
||||
|
||||
$ echo ${string:(-4)}
|
||||
Cabc
|
||||
```
|
||||
|
||||
|
||||
|
||||
Remplacement de sous-chaine:
|
||||
|
||||
```bash
|
||||
$ string=abcABC123ABCabc
|
||||
|
||||
$ echo ${string/abc/xyz} # Remplace la 1ere sous-chaine 'abc' par 'xyz'.
|
||||
xyzABC123ABCabc
|
||||
|
||||
$ echo ${string//abc/xyz} # Remplace toutes les sous-chaine 'abc' par 'xyz'.
|
||||
xyzABC123ABCxyz
|
||||
|
||||
match=abc
|
||||
repl=000
|
||||
echo ${string/$match/$repl}
|
||||
000ABC123ABCabc
|
||||
|
||||
echo ${string//$match/$repl}
|
||||
000ABC123ABC000
|
||||
|
||||
|
||||
echo ${stringZ/abc} # Supprime la 1ere sous-chaine 'abc'
|
||||
ABC123ABCabc
|
||||
echo ${stringZ//abc} # Supprime toutes les sous-chaine 'abc'
|
||||
ABC123ABC
|
||||
|
||||
```
|
||||
|
||||
http://tldp.org/LDP/abs/html/string-manipulation.html
|
||||
|
||||
Reference in New Issue
Block a user