tech:notes_sed_grep_regex
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| tech:notes_sed_grep_regex [2025/03/24 15:06] – créée - modification externe 127.0.0.1 | tech:notes_sed_grep_regex [2025/12/15 16:21] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| {{tag> | {{tag> | ||
| - | = Notes sed grep regex | + | # Notes sed grep regex |
| - | == grep | + | ## grep |
| Voir : | Voir : | ||
| + | * `man regex` | ||
| * [[grep avec option p comme AIX - Récupérer un paragraphe complet]] | * [[grep avec option p comme AIX - Récupérer un paragraphe complet]] | ||
| + | Voir aussi : | ||
| + | * pcregrep | ||
| - | + | ## Regex | |
| - | == Regex | + | |
| Exclude | Exclude | ||
| - | <code -> | + | ~~~ |
| ^((? | ^((? | ||
| - | </ | + | ~~~ |
| + | |||
| + | |||
| + | Grepper sur plusieurs lignes (match grep with line break) | ||
| + | ~~~bash | ||
| + | grep -zP ' | ||
| + | ~~~ | ||
| - | === Extended regular expressions | + | ### Extended regular expressions |
| Source : https:// | Source : https:// | ||
| Ligne 24: | Ligne 34: | ||
| Examples: | Examples: | ||
| - | <code -> | + | ~~~ |
| abc? | abc? | ||
| becomes ‘abc\?’ when using extended regular expressions. It matches the literal string ‘abc?’. | becomes ‘abc\?’ when using extended regular expressions. It matches the literal string ‘abc?’. | ||
| Ligne 35: | Ligne 45: | ||
| \(abc*\)\1 | \(abc*\)\1 | ||
| becomes ‘(abc*)\1’ when using extended regular expressions. Backreferences must still be escaped when using extended regular expressions. | becomes ‘(abc*)\1’ when using extended regular expressions. Backreferences must still be escaped when using extended regular expressions. | ||
| - | </ | + | ~~~ |
| - | === Pb | + | Trouver des tabulations dans un fichier (GNU grep) you can use the Perl-style regexp |
| + | ~~~bash | ||
| + | grep -P ' | ||
| + | ~~~ | ||
| - | ==== Pb Binary file daemon.log matches | ||
| + | ### Pb | ||
| - | < | + | #### Pb binary file matches |
| + | |||
| + | |||
| + | ~~~bash | ||
| # grep -i -e ' | # grep -i -e ' | ||
| Binary file daemon.log matches | Binary file daemon.log matches | ||
| - | </ | + | ~~~ |
| Solution | Solution | ||
| - | < | + | ~~~bash |
| grep -a -i -e ' | grep -a -i -e ' | ||
| - | </code> | + | ~~~ |
| + | |||
| + | |||
| + | ## head / tail | ||
| + | |||
| + | Supprimer les deux dernières lignes | ||
| + | ~~~bash | ||
| + | head -n -2 myfile.txt | ||
| + | ~~~ | ||
| + | |||
| + | Supprimer les 4 primières lignes | ||
| + | ~~~bash | ||
| + | tail -n +4 myfile.txt | ||
| + | ~~~ | ||
| - | == Sed | + | ## Sed |
| Voir : | Voir : | ||
| Ligne 58: | Ligne 88: | ||
| Matched text | Matched text | ||
| - | < | + | ~~~bash |
| sed -i -e ' | sed -i -e ' | ||
| - | </ | + | ~~~ |
| Commenter tout un fichier | Commenter tout un fichier | ||
| - | < | + | ~~~bash |
| sed -i -e ' | sed -i -e ' | ||
| - | </code> | + | ~~~ |
| + | |||
| + | |||
| + | Supprimer tous les espaces en début de ligne | ||
| + | ~~~bash | ||
| + | sed -e 's/^\s\+// | ||
| + | ~~~ | ||
| Première lettre en majuscule | Première lettre en majuscule | ||
| - | < | + | ~~~bash |
| sed -e ' | sed -e ' | ||
| - | </ | + | ~~~ |
| Insérer une ligne au début d'un fichier | Insérer une ligne au début d'un fichier | ||
| - | < | + | ~~~bash |
| sed -i ' | sed -i ' | ||
| - | </ | + | ~~~ |
| Ou pour insérer le caractère ' | Ou pour insérer le caractère ' | ||
| - | < | + | ~~~bash |
| sed -e '1 i\{' | sed -e '1 i\{' | ||
| - | </ | + | ~~~ |
| Strip HTML | Strip HTML | ||
| - | < | + | ~~~bash |
| sed -e ' | sed -e ' | ||
| - | </ | + | ~~~ |
| Colonnes | Colonnes | ||
| - | < | + | ~~~bash |
| df -PhT |column -t | df -PhT |column -t | ||
| grep -v -e ' | grep -v -e ' | ||
| - | </ | + | ~~~ |
| Adresse IP | Adresse IP | ||
| - | < | + | ~~~bash |
| rgrep -E --color -e ' | rgrep -E --color -e ' | ||
| - | </ | + | ~~~ |
| Supprimer une ligne | Supprimer une ligne | ||
| - | < | + | ~~~bash |
| sed -i -e '/ | sed -i -e '/ | ||
| - | </ | + | ~~~ |
| Supprimer toutes les lignes à partir du motif | Supprimer toutes les lignes à partir du motif | ||
| - | < | + | ~~~bash |
| sed -e '/ | sed -e '/ | ||
| - | </ | + | ~~~ |
| **$$** : jusqu' | **$$** : jusqu' | ||
| Ligne 114: | Ligne 151: | ||
| Source et explications : https:// | Source et explications : https:// | ||
| - | < | + | ~~~bash |
| sed ': | sed ': | ||
| - | </ | + | ~~~ |
| Afficher de la ligne n à la ligne m : | Afficher de la ligne n à la ligne m : | ||
| - | < | + | ~~~bash |
| cat -n launch.sh | cat -n launch.sh | ||
| # ou | # ou | ||
| Ligne 127: | Ligne 164: | ||
| # Puis (de la ligne 46 à la ligne 68) | # Puis (de la ligne 46 à la ligne 68) | ||
| cat launch.sh | sed -n -e ' | cat launch.sh | sed -n -e ' | ||
| - | </ | + | ~~~ |
| - | === Character Classes | + | ### Character Classes |
| how to represent " | how to represent " | ||
| Ligne 148: | Ligne 185: | ||
| - | == Awk | + | ## Awk |
| Voir : | Voir : | ||
| * https:// | * https:// | ||
| + | |||
| + | Utiliser les variables d' | ||
| + | ~~~bash | ||
| + | awk -v a=" | ||
| + | ~~~ | ||
| Dernier champ ; avant dernier champ | Dernier champ ; avant dernier champ | ||
| - | < | + | ~~~bash |
| awk ' | awk ' | ||
| awk ' | awk ' | ||
| - | </ | + | ~~~ |
| Mettre en majuscule/ | Mettre en majuscule/ | ||
| - | < | + | ~~~bash |
| awk '/ | awk '/ | ||
| - | </ | + | ~~~ |
| Exemple ligne commençant par '' | Exemple ligne commençant par '' | ||
| - | < | + | ~~~bash |
| apt-cache search opencv |awk '/ | apt-cache search opencv |awk '/ | ||
| - | </ | + | ~~~ |
| Trouver les zombies | Trouver les zombies | ||
| - | < | + | ~~~bash |
| ps aux | awk '$8 ~ / | ps aux | awk '$8 ~ / | ||
| - | </ | + | ~~~ |
| Remplacer un motif par un autre | Remplacer un motif par un autre | ||
| - | <code -> | + | ~~~ |
| $ echo "Bobby is cool" | awk ' | $ echo "Bobby is cool" | awk ' | ||
| Teddy is cool | Teddy is cool | ||
| - | </ | + | ~~~ |
| Exemple | Exemple | ||
| - | < | + | ~~~bash |
| ip link |awk '/: br-/ { gsub(":", | ip link |awk '/: br-/ { gsub(":", | ||
| - | </ | + | ~~~ |
| Awk One-Liners - Remove duplicate, nonconsecutive lines | Awk One-Liners - Remove duplicate, nonconsecutive lines | ||
| - | < | + | ~~~bash |
| iptables-save |awk ' !x[$0]++' | iptables-save |awk ' !x[$0]++' | ||
| - | </ | + | ~~~ |
| sum - total - faire l' | sum - total - faire l' | ||
| - | < | + | ~~~bash |
| awk ' | awk ' | ||
| - | </ | + | ~~~ |
| if greater / less than | if greater / less than | ||
| - | < | + | ~~~bash |
| awk -F':' | awk -F':' | ||
| - | </ | + | ~~~ |
| Avant-dernier champs | Avant-dernier champs | ||
| - | < | + | ~~~bash |
| awk '{ print ( $(NF-1) ) }' | awk '{ print ( $(NF-1) ) }' | ||
| - | </ | + | ~~~ |
| Remplacer un motif par un autre (remplace) | Remplacer un motif par un autre (remplace) | ||
| - | < | + | ~~~bash |
| awk '/ | awk '/ | ||
| - | </ | + | ~~~ |
| Lire une valeur dans un fichier ini en supprimant les espaces | Lire une valeur dans un fichier ini en supprimant les espaces | ||
| - | <code ini config.ini> | + | '' |
| + | ~~~ini | ||
| process_name = appsrvd | process_name = appsrvd | ||
| - | </ | + | ~~~ |
| - | < | + | ~~~bash |
| awk -F= '/ | awk -F= '/ | ||
| - | </ | + | ~~~ |
| Calcul | Calcul | ||
| - | < | + | ~~~bash |
| calc() { awk "BEGIN { print $* }"; } | calc() { awk "BEGIN { print $* }"; } | ||
| calc_sum() { awk ' | calc_sum() { awk ' | ||
| - | </code> | + | ~~~ |
| + | |||
| + | ### Awk autres | ||
| + | |||
| + | '' | ||
| + | ~~~bash | ||
| + | # ......... | ||
| + | $SMBCLIENT -gNL $key 2>/dev/null| awk -v key=" | ||
| + | BEGIN { ORS=""; | ||
| + | / | ||
| + | if (first) | ||
| + | print opts; first=0 | ||
| + | dir = $2 | ||
| + | loc = $2 | ||
| + | # Enclose mount dir and location in quotes | ||
| + | # Double quote " | ||
| + | gsub(/\$$/, " | ||
| + | print " \\\n\t \"/" | ||
| + | } | ||
| + | END { if (!first) print " | ||
| + | ' | ||
| + | ~~~ | ||
| - | == Python | + | ## Python |
| **re.match** \\ | **re.match** \\ | ||
| Ligne 244: | Ligne 308: | ||
| - | == Exemple de regex | + | ## Exemple de regex |
| - | < | + | ~~~python |
| email_re = re.compile(r' | email_re = re.compile(r' | ||
| - | </ | + | ~~~ |
| - | == Autres | + | ## Autres |
| - | === Tr | + | ### Tr |
| SC1017 (error): Literal carriage return. Run script through | SC1017 (error): Literal carriage return. Run script through | ||
| - | < | + | ~~~bash |
| tr -d ' | tr -d ' | ||
| - | </ | + | ~~~ |
| Convertir les fins de ligne en null char | Convertir les fins de ligne en null char | ||
| - | < | + | ~~~bash |
| tr ' | tr ' | ||
| - | </ | + | ~~~ |
| - | === Preserve file timestamp in multifile string replace | + | ### Preserve file timestamp in multifile string replace |
| Source : https:// | Source : https:// | ||
| - | < | + | ~~~bash |
| # -I to grep ignores binary files, | # -I to grep ignores binary files, | ||
| # @ in sed works as separator for strings with / | # @ in sed works as separator for strings with / | ||
| Ligne 274: | Ligne 338: | ||
| grep -IR aaaa /somepath/ | awk -F: ' | grep -IR aaaa /somepath/ | awk -F: ' | ||
| - | </ | + | ~~~ |
tech/notes_sed_grep_regex.1742825205.txt.gz · Dernière modification : de 127.0.0.1
