1.Expression reguliere

Regular expression features available in sed include 

  • char A
    • single ordinary character matches itself.
  • *
    • Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be an ordinary character, a special character preceded by\, a., a grouped regexp (see below), or a bracket expression.
  • \+As
    • As*, but matches one or more. It is aGNUextension.
  • \?
    • As*, but only matches zero or one. It is aGNUextension.
  • \{i\}
    • As*, but matches exactlyisequences (iis a decimal integer; for portability, keep it between 0 and 255 inclusive).
  • \{i,j\}
    • Matches between i and j, inclusive, sequences.
  • \{i,\}
    • Matches more than or equal to i sequences.
  • \(regexp\)
    • Groups the innerregexpas a whole, this is used to:
  • .
    • Matches any character, including newline.
  • ^
    • Matches the null string at beginning of the pattern space, i.e. what appears after the circumflex must appear at the beginning of the pattern space.
  • $
    • It is the same as^, but refers to end of pattern space.$also acts as a special character only at the end of the regular expression.
  • regexp1\|regexp2
    • Matches eitherregexp1orregexp2. Use parentheses to use complex alternative regular expressions. The matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. It is aGNUextension.
  •  \digit
    • Matches thedigit-th\(...\)parenthesized subexpression in the regular expression. This is called aback reference. Subexpressions are implicity numbered by counting occurrences of\(left-to-right.
  • \n
    • Matches the newline character.
  • \char
    • Matcheschar, wherecharis one of$,*,.,[,\, or^. Note that the only C-like backslash sequences that you can portably assume to be interpreted are\nand\\; in particular\tis not portable, and matches a ‘t’ under most implementations ofsed, rather than a tab character.


Predefined character classes supported by sed include [:alpha:], [:blank:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].

1.supprimer les lignes vides d'un fichier

sed -e '4d; 7d' test.txt

2. supprimer les lignes x à y

sed -e 'x, yd' test.txt 

3.Supprimer les balises html/xml

sed -re ':start s/<[^>]*>//g; /</ {N; b start}' fichier.html 

4. sed @ delimiter: Substitute /opt/omni/lbin to /opt/tools/bin

When you substitute a path name which has '/', you can use any delimiter as a delimiter instead of '/'. In the sed example below, in the last line of the input file, /opt/omni/lbin was changed to /opt/tools/bin.

$ sed 's@/opt/omni/lbin@/opt/tools/bin@g' path.txt

 5. sed & Usage: Concat to matching pattern

5.1. Exemple 1

$ sed 's@/usr/bin@&/local@g' path.txt
/usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin/local:/usr/sas/bin /usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin/local:/opt/omni/bin: /opt/omni/lbin:/opt/omni/sbin:/root/bin

 se sed concatene à la string /usr/bin, la chaine /local

5.2. Exemple 2

$ sed 's@^.*$@<<<&>>>@g' path.txt 

6.  Extraire un path d'une string

$ cat myfile.log
Caused by: /mnt/nfs/ged/upload_sigale/primos/001/1409/KBIS.pdf (Aucun fichier ou dossier de ce type)
$ sed 's|.* \(/.*\) (Aucun.*|\1|' myfile.log


7.Groupe et Back-references

 Grouping can be used in sed like normal regular expression. A group is opened with “\(” and closed with “\)”.Grouping can be used in combination with back-referencing.

Back-reference is the re-use of a part of a Regular Expression selected by grouping. Back-references in sed can be used in both a Regular Expression and in the replacement part of the substitute command.

6.1.Example 1: Get only the first path in each line

$ sed 's/\(\/[^:]*\).*/\1/g' path.txt 

 In the above example, \(\/[^:]*\) matches the path available before first : comes. \1 replaces the first matched group.






Add comment

Security code

Go to Top
Template by JoomlaShine