tech:notes_sed_grep_regex
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:notes_sed_grep_regex [2025/05/04 15:46] – Jean-Baptiste | tech:notes_sed_grep_regex [2026/05/20 10:45] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes sed grep regex | ||
| + | |||
| + | ## grep | ||
| + | |||
| + | Voir : | ||
| + | * `man regex` | ||
| + | * [[grep avec option p comme AIX - Récupérer un paragraphe complet]] | ||
| + | * https:// | ||
| + | |||
| + | Voir aussi : | ||
| + | * Ripgrep | ||
| + | * pcregrep | ||
| + | |||
| + | |||
| + | ## Grep autres | ||
| + | |||
| + | `--line-buffered` | ||
| + | |||
| + | ~~~bash | ||
| + | tail -F / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Regex | ||
| + | |||
| + | Exclude | ||
| + | ~~~ | ||
| + | ^((? | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Grepper sur plusieurs lignes (match grep with line break) | ||
| + | ~~~bash | ||
| + | grep -zP ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Extended regular expressions | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | The only difference between basic and extended regular expressions is in the behavior of a few characters: ''?'', | ||
| + | |||
| + | Examples: | ||
| + | ~~~ | ||
| + | abc? | ||
| + | becomes ‘abc\?’ when using extended regular expressions. It matches the literal string ‘abc?’. | ||
| + | c\+ | ||
| + | becomes ‘c+’ when using extended regular expressions. It matches one or more ‘c’. | ||
| + | a\{3,\} | ||
| + | becomes ‘a{3,}’ when using extended regular expressions. It matches three or more ‘a’. | ||
| + | \(abc\)\{2, | ||
| + | becomes ‘(abc){2, | ||
| + | \(abc*\)\1 | ||
| + | becomes ‘(abc*)\1’ when using extended regular expressions. Backreferences must still be escaped when using extended regular expressions. | ||
| + | ~~~ | ||
| + | |||
| + | Trouver des tabulations dans un fichier (GNU grep) you can use the Perl-style regexp | ||
| + | ~~~bash | ||
| + | grep -P ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ### Pb | ||
| + | |||
| + | #### Pb binary file matches | ||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | # grep -i -e ' | ||
| + | Binary file daemon.log matches | ||
| + | ~~~ | ||
| + | |||
| + | Solution | ||
| + | ~~~bash | ||
| + | grep -a -i -e ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Bash | ||
| + | |||
| + | Pour plus de lisibilité il est recommandé de mettre les regex dans des fonctions bien nommées et commentées | ||
| + | |||
| + | ~~~bash | ||
| + | regex_keep_only_file_extension() { | ||
| + | # Ex: in ' | ||
| + | # out ' | ||
| + | sed -e ' | ||
| + | } | ||
| + | |||
| + | get_list() { | ||
| + | local File | ||
| + | for File in $(get_files_from_list); | ||
| + | echo " | ||
| + | done | regex_keep_only_file_extension | sort -u | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## 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 | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | Matched text | ||
| + | ~~~bash | ||
| + | sed -i -e ' | ||
| + | ~~~ | ||
| + | |||
| + | Commenter tout un fichier | ||
| + | ~~~bash | ||
| + | sed -i -e ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Supprimer tous les espaces en début de ligne | ||
| + | ~~~bash | ||
| + | sed -e ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Première lettre en majuscule | ||
| + | ~~~bash | ||
| + | sed -e ' | ||
| + | ~~~ | ||
| + | |||
| + | Insérer une ligne au début d'un fichier | ||
| + | ~~~bash | ||
| + | sed -i ' | ||
| + | ~~~ | ||
| + | |||
| + | Ou pour insérer le caractère ' | ||
| + | ~~~bash | ||
| + | sed -e '1 i\{' | ||
| + | ~~~ | ||
| + | |||
| + | Strip HTML | ||
| + | ~~~bash | ||
| + | sed -e ' | ||
| + | ~~~ | ||
| + | |||
| + | Colonnes | ||
| + | ~~~bash | ||
| + | df -PhT | column -t | ||
| + | grep -v -e ' | ||
| + | ~~~ | ||
| + | |||
| + | Adresse IP | ||
| + | ~~~bash | ||
| + | rgrep -E --color -e ' | ||
| + | ~~~ | ||
| + | |||
| + | Supprimer une ligne | ||
| + | ~~~bash | ||
| + | sed -i -e '/ | ||
| + | ~~~ | ||
| + | |||
| + | Supprimer toutes les lignes à partir du motif | ||
| + | ~~~bash | ||
| + | sed -e '/ | ||
| + | ~~~ | ||
| + | |||
| + | **$$** : jusqu' | ||
| + | |||
| + | Supprimer les fins de ligne | ||
| + | |||
| + | Source et explications : https:// | ||
| + | |||
| + | ~~~bash | ||
| + | sed ': | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Afficher de la ligne n à la ligne m : | ||
| + | ~~~bash | ||
| + | cat -n launch.sh | ||
| + | # ou | ||
| + | grep -n -A7 my_function launch.sh | ||
| + | |||
| + | # Puis (de la ligne 46 à la ligne 68) | ||
| + | cat launch.sh | sed -n -e ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Character Classes | ||
| + | |||
| + | how to represent " | ||
| + | |||
| + | That will be this character class: | ||
| + | |||
| + | '' | ||
| + | |||
| + | Which means allow one of these: | ||
| + | |||
| + | - Alpha numeric | ||
| + | - Underscore | ||
| + | - Hyphen | ||
| + | |||
| + | It is important to keep hyphen at 1st or last position in character class to avoid escaping. | ||
| + | |||
| + | |||
| + | |||
| + | ## Awk | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | Utiliser les variables d' | ||
| + | ~~~bash | ||
| + | awk -v a=" | ||
| + | ~~~ | ||
| + | |||
| + | Dernier champ ; avant dernier champ | ||
| + | ~~~bash | ||
| + | awk ' | ||
| + | |||
| + | awk ' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Mettre en majuscule/ | ||
| + | ~~~bash | ||
| + | awk '/ | ||
| + | ~~~ | ||
| + | |||
| + | Exemple ligne commençant par '' | ||
| + | ~~~bash | ||
| + | apt-cache search opencv | awk '/ | ||
| + | ~~~ | ||
| + | |||
| + | Trouver les zombies | ||
| + | ~~~bash | ||
| + | ps aux | awk '$8 ~ / | ||
| + | ~~~ | ||
| + | |||
| + | Remplacer un motif par un autre | ||
| + | ~~~ | ||
| + | $ echo "Bobby is cool" | awk ' | ||
| + | Teddy is cool | ||
| + | ~~~ | ||
| + | |||
| + | Exemple | ||
| + | ~~~bash | ||
| + | ip link | awk '/: br-/ { gsub(":", | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Awk One-Liners - Remove duplicate, nonconsecutive lines | ||
| + | ~~~bash | ||
| + | iptables-save | awk ' !x[$0]++' | ||
| + | ~~~ | ||
| + | |||
| + | sum - total - faire l' | ||
| + | ~~~bash | ||
| + | awk ' | ||
| + | ~~~ | ||
| + | |||
| + | if greater / less than | ||
| + | ~~~bash | ||
| + | awk -F':' | ||
| + | ~~~ | ||
| + | |||
| + | Avant-dernier champs | ||
| + | ~~~bash | ||
| + | awk '{ print ( $(NF-1) ) }' | ||
| + | ~~~ | ||
| + | |||
| + | Remplacer un motif par un autre (remplace) | ||
| + | ~~~bash | ||
| + | awk '/ | ||
| + | ~~~ | ||
| + | |||
| + | Lire une valeur dans un fichier ini en supprimant les espaces | ||
| + | '' | ||
| + | ~~~ini | ||
| + | process_name = appsrvd | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | awk -F= '/ | ||
| + | ~~~ | ||
| + | |||
| + | Calcul | ||
| + | ~~~bash | ||
| + | calc() { awk "BEGIN { print $* }"; } | ||
| + | |||
| + | calc_sum() { awk ' | ||
| + | ~~~ | ||
| + | |||
| + | ### Awk autres | ||
| + | |||
| + | ''/ | ||
| + | ~~~bash | ||
| + | # ......... | ||
| + | $SMBCLIENT -gNL $key 2>/ | ||
| + | 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 | ||
| + | |||
| + | **re.match** \\ | ||
| + | La méthode match recherche une correspondance uniquement au début de la chaîne | ||
| + | |||
| + | **re.search** \\ | ||
| + | search() La fonction recherchera le modèle d’expression régulière et renverra la première occurrence. \\ | ||
| + | Contrairement à Python re.match(), il vérifiera toutes les lignes de la chaîne d' | ||
| + | |||
| + | **re.findall** \\ | ||
| + | findall() Le module est utilisé pour rechercher « toutes » les occurrences qui correspondent à un modèle donné. En revanche, le module search() ne renverra que la première occurrence correspondant au modèle spécifié. \\ | ||
| + | findall() parcourra toutes les lignes du fichier et renverra toutes les correspondances de modèle qui ne se chevauchent pas en une seule étape. | ||
| + | |||
| + | |||
| + | ## Exemple de regex | ||
| + | |||
| + | ~~~python | ||
| + | email_re = re.compile(r' | ||
| + | ~~~ | ||
| + | |||
| + | ## Autres | ||
| + | |||
| + | ### Tr | ||
| + | |||
| + | SC1017 (error): Literal carriage return. Run script through | ||
| + | ~~~bash | ||
| + | tr -d ' | ||
| + | ~~~ | ||
| + | |||
| + | Convertir les fins de ligne en null char | ||
| + | ~~~bash | ||
| + | tr ' | ||
| + | ~~~ | ||
| + | |||
| + | ### Preserve file timestamp in multifile string replace | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | ~~~bash | ||
| + | # -I to grep ignores binary files, | ||
| + | # @ in sed works as separator for strings with / | ||
| + | # -r in touch uses existing file timestap as a reference | ||
| + | |||
| + | grep -IR aaaa /somepath/ | awk -F: ' | ||
| + | ~~~ | ||
| + | |||
