Certification Linux LPI/Administrateur système débutant/Examen 101/GNU et commandes Unix/Expressions régulières

Une page de Wikiversité.

WP-TranslationProject TwoFlags.svg Il a été demandé de traduire cette page depuis (en)



Faire des recherches dans des fichiers textes à l'aide des expressions régulières
Computer-aj aj ashton 01.svg
Chapitre 7
Leçon : Certification Linux LPI/Administrateur système débutant/Examen 101/GNU et commandes Unix
Chap. préc. : Modifier les priorités d'exécution des tâches
Chap. suiv. : Éditions élémntaires de fichiers à l'aide de VI
Icon falscher Titel.svg

En raison de limitations techniques, la typographie souhaitable du titre, « Certification Linux LPI/Administrateur système débutant/Examen 101/GNU et commandes Unix : Faire des recherches dans des fichiers textes à l'aide des expressions régulières
Certification Linux LPI/Administrateur système débutant/Examen 101/GNU et commandes Unix/Expressions régulières
 », n'a pu être restituée correctement ci-dessus.

Sommaire

[modifier] Objectifs

Description: Les candidats devraient pouvoir manipuler des fichiers et des données textes en utilisant les expressions réguliéres. Cet objectif inclu la création simple d'expression réguliéres contenant plusieurs éléments d'écriture. Ceci inclu aussi l'utilisation réguliére d'outils pour réaliser des recherches à travers un filesystem ou le contenu d'un fichier.

Key files terms and utilities include:
grep
regexp
sed

[modifier] Pattern matching

There are two kinds of pattern matching:

  • Wildcards (File Name Generation)
  • Regexp (Regular Expression)

Wildcard characters are mainly applied when they are used in the current directory or subdirectories. When wildcard characters *, ?, [ - ], ~, and ! are used in regexp they no longer generate filenames.

Some of the utilities that us regexp are:

  • grep, egrep
  • vi
  • more
  • sed
  • Perl

Limited regexp search pattern Used by all utilities using regexp.

  • Any 1 char . Ab.a Abla or Abca
  • 1 char set [ ] Ab[sd]a Absa or Abda only
  • 1 char range [ - ] Ab[a-z]a Abaa or Abba or ...
  • Not in set [^ ] Ab[^0-9]a Abaa or Abba or ...
  • 0 or more * Ab*a Absala or Aba or ...
  • Begin line ^ ^Aba Line starts>Aba
  • End line $ Aba$ Aba<line ends
  • Literal \ Aba\$ Aba$

Example:

Ab[0-3]s
^Ab\^bA
[01]bin$
^..\\
[^zZ]oro

Combinations of limited regexp Combinaison used by all utilities using regexp.

  • Any string .* Ab.*a Abrahma or Abaa or ...
  • String from [ ]* th[aersti]* There or This or ...
  • Multi range [ - - ] Ab[0-2][a-c]a Ab0aa or Ab1aa or ...
  • Match \ \\ \\[a-zA-Z]* \Bethoven

Examples:

Ab[0-3][a-z]s
...$
^[01]\^2
[0-9][a-z] \$
[a-zA-Z]*
^[^c-zC-Z]*
^[a-zA-Z0-9]$

Modifier patterns Replace strings matched by regexp patterns

  • Match m \{m\} b[0-9]\{3\} b911
  • One or more \{m,\} b[0-9]\{2,\} b52
  • Up to n \{m,n\} b[0-9]\{2,4\} b1234
  • Begining of word \< \<wh where
  • End of word \> [0-9]\> bin01

[modifier] grep

To find text in a file, use grep.

grep [options] [string] [files]

Best to quote the string to prevent interpretation.

Common options: -i: Ignore case -l: List filename only if at least one matches -c: Display only count of matched lines -n: Also display line number -v: Must not match.

Examples:

grep host /etc/*.conf
grep -l '\<mai' /usr/include/*.h
grep -n toto /etc/group
grep -vc root /etc/passwd
grep '^user' /etc/passwd
grep '[rR].*' /etc/passwd
grep '\<[rR].*' /etc/passwd

[modifier] sed

To apply a command on a stream, use sed.

sed [address1][,address2][!]command[options] [files...]

The program sed will apply a command from address1 to address2 in a file. The address1 and address2 format are regular expressions. The sed program is a noninteractive editing tool.

Examples:

sed '1,3s/aa/bb/g' file # Replace in file from line 1 to 3 aa by bb.
sed '/here/,$d' file # Delete line from here to the end.
sed '/here/d' file # Delete lines with word here.
sed '1,/xxx/p' file # Print lines from 1 to xxx.
sed '/ll/,/ff/!s/maison/house/g' file # In file replace words maison by house excluding lines from ll to ff.

[modifier] Exercices

  1. Copy all the files from /etc in your home direcory etc/. Display the contents of all the *.conf files by replacing the word host by machine.
  2. Display the contents of all the *.conf files that don't contain the root word. What is the command using grep and sed?
  3. Print out all the group names where root belongs to.
  4. List all the group names that are 4 or 5 characters long.
  5. List all the files that contain character lines without spaces (blank).
  6. List in etc/ directory all the files that contain numerical characters.
  7. Print with ls only the directory names in /.
  8. Do “ps -aux” and replace user r_polto by root and print it in to a file called new_process.txt
  9. List all process called apache and owned by a usernames starting by “p” or “P”.