Red Hat Enterprise Linux




Chapters     |     Exercises     |     Workshop     |    

Files - filters


Filters

Les filtres sont des outils qui permettent d’afficher le contenu les fichiers en filtrant les infos qu’on désire

Filtres list


grep       |       sed       |       tail       |       head       |       tr      |       sort

[root@lab ~]# tail file show 10 last lines of file

[root@lab ~]# head file show 10 first lines of file

[root@lab ~]# tr arg1 arg2 file change arg1 to arg2 in Output

[root@lab ~]# sort file classify lines in alphabetic order. Sorts the output of a command or file

[root@lab ~]# uniq file Eliminate double lines. Lists all the unique lines in a file or command output

[root@redhat ~]# tac file

Grep grep Command

Syntax

[root@lab ~]# grep var file only lines which contains var view

Matching Using Symbols

[root@lab ~]# grep ^var file lines which begins with var

[root@lab ~]# grep var$ file lines which ends with var

[root@lab ~]# grep ^var$ file If users wanted to make sure that the pattern was the only thing on a line, use both the beginning and end of line anchors

[root@lab ~]# grep ^var$ file lines which countains only var

Regular Expressions

  1. x.x : Any Character

    Regular expressions use a . as the unrestricted wildcard character. A regular expression of c . t will look for data containing a c, followed by anyone character, followed by a t . Examples of data that would match this regular expression's pattern a re cat. cot, and cut , but also c5t and cot.

  2. x.*x Many Characters

    If a regular expression of c.*t was used, it would match ct, cat, coat, culvert, etc.; any data that started with a c, then zero to infinitely many characters, ending with at.

  3. x[yyyyy]x Specific Characters

    Another type of wild card used in regular expressions is a set of acceptable characters at a specific character position. When using an unrestricted wildcard, users could not predict the character that would match the wildcard; however, if users wanted to only match the words cat. cot, and cut. but not o d d items like c5t or cQt, replace the unrestricted wildcard with one where acceptable characters are specified. If the regular expression was changed to c[aou]t , it would be specifying that the regular expression should match patterns that start with a c , are followed by an a or an o or a u , followed by a t .

  4. Multipliers are a mechanism used often with wildcards. Multipliers apply to the previous character in the regular expression. One of the more common multipliers used is * . A * , when used in a regular expression, modifies the previous character to mean zero to infinitely many of that character.

Examples

[root@lab ~]# grep -v bash /etc/passwd

[root@lab ~]# grep -n bash /etc/passwd

[root@lab ~]# grep -x TOGO File1.csv Display Lines which contains only "TOGO"

[root@lab ~]# grep ^user1 /etc/passwd Display Lines begining with "user1" in /etc/passwd

[root@lab ~]# grep bash$ /etc/passwd Display Users using bash as Default shell

[root@lab ~]# grep ^TOGO$ File1 Display Lines which contains only "TOGO"

[root@lab ~]# grep b.h /etc/passwd

[root@lab ~]# grep b..h /etc/passwd

[root@redhat ~]# grep b.* file

[root@redhat ~]# grep b.*h file


Streamline Editor (sed)

sed 's/var1/var2/g' file change var1 to var2 in show

sed 2q file displays the first two lines

sed -n /text/p file prints all lines containing the text

sed -n /^text/p file Shows all lines that not contain the text

sed -n /./p file Shows all lines that contain at least one character

sed -n /\./p file Shows all lines that contain a dot

sed s/text1/text2/g file replaces the string text1 with the string text2 and writes the results to the standard output

sed -i s/text1/text2/g file Writing it to the standard output is very secure, but it doesn’t apply a single change to the file itself. If you want to do that, add the -i option to the command


Eninam BAGNABANA     |         |     bagnadavid@outlook.fr     |