Red Hat Certified System Administration 8



Chapter 04: Working with Text Files

Goal

In this chapter, you learn how to work with text files. The chapter teaches you how to create text files, but also how to look for specific content in the different text files.


Sommaire

  • Creating Files
  • Viewing Files
  • Pipes & Redirections
  • Editing Files
  • Usual Commands

Creating Files



Creating Files Touch command Vi Command

Viewing Files




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

Pipes & Redirections



Channels


pipes

Piping is used to send the result of a command to another command

command1 | command2 redirect command1 output to command2 input
command1 || command2 command1 runs; in error case command2 runs
command1 && command2 command2 runs only if command1 runs correctly


Examples [user1@redhat ~]$ head /etc/passwd | nl

[user1@redhat ~]$ head /etc/password || echo "Ce Fichier n'existe pas"
[user1@redhat ~]$ head /etc/passwd || echo "Ce Fichier n'existe pas"


Redirections

redirection sends the output of a command to a file. This file doesn’t necessarily need to be a regular file, but it can also be a device file

Redirections

command > file redirect command result in a file
command >> file don’t overwrite file content but add result at the end
command < file change standard input of command to a file content
command << file
command 2> file errors redirection
command 2> /dev/null discard stderr error messages by redirecting to /dev/null
command >& file Redirect STDOUT and STDERR to specified file. If file exists, overwrite it. If it does not exist, create it.

command &> file Redirect STDOUT and STDERR to specified file. If file exists, overwrite it. If it does not exist, create it.

command &>> file Redirect STDOUT and STDERR to specified file. If file exists, append to it. If it does not exist, create it.

[root@redhat ~]# cat /home 2> red2
[root@redhat ~]# cat /home 2>> red2

[root@redhat ~]# head < /etc/passwd
Examples [root@lab ~]# mkdir RH
[root@lab ~]# touch RH/File1
[root@lab ~]# echo "LINUX OS" > RH/File1 Redirect "LINUX 0S" to File1
[root@lab ~]# cat RH/File1
[root@lab ~]# echo "WELCOME" > RH/File1
[root@lab ~]# cat RH/File1 "WELCOME" overwrite "LINUX OS"
[root@lab ~]# echo "LINUX" >> RH/File1
[root@lab ~]# cat RH/File1
[root@lab ~]# touch RH/File2
[root@lab ~]# ls -la 2> RH/File2
[root@lab ~]# cat RH/File2 Empty
[root@lab ~]# cat /home/ 2> RH/File2
[root@lab ~]# cat RH/File2
[root@lab ~]# touch RH/File3
[root@lab ~]# ls -la >& RH/File3
[root@lab ~]# cat RH/File3
[root@lab ~]# cat /home/ >& RH/File3
[root@lab ~]# cat RH/File3
[root@lab ~]# ls -la &> RH/File3
[root@lab ~]# cat RH/File3
[root@lab ~]# cat /home/ &> RH/File3
[root@lab ~]# cat RH/File3
[root@lab ~]#
[user1@redhat ~]$ head < /etc/passwd | nl

Editing Files


Files Editors

  • Gedit
  • touch
  • Vi
  • Vim
  • Nano

VI Improved (vim)

vi stands for Visual Interface.
VI Improved (vim)

VI IMproved

The version of vi that is shipped with Red Hat Enterprise Linux 7 is called vim. vim stands for VI IMproved, as vim comes with many features not found in the original vi, while still remaining (mostly) backward-compatible. Among the new features are popular options, such as

  • syntax highlighting,
  • completion modes,
  • and spell-checking.

Syntax

[root@lab ~]# vi file

[root@lab ~]# vim options file

■ Open and edit a file.
■ Creates file if it doesn’t exist

Execution modes

Vim functions in three different modes:
  • command mode
  • insert mode
  • last line mode.

Editing Steps

When you first start working with a file, you are in command mode. Here, you can issue commands that allow you to move around your file without actually inserting text into the file.

When you’re ready to insert text into the file, you can use i or a to move into insert mode, enabling you to insert text at the cursor location. To move out of insert mode back into command mode, just press the Esc key.

The last mode you can enter is last line mode, which you enter by typing a colon (:). In this mode, you can issue additional commands to save, quit, and do even more.

esc [key] to quit mode

Commands mode

i insert (insert mode). insert text at the current cursor position
a append (edit at end of line). append text after the current position of the cursor
v (visual) selection
d to cut the selection.
y to copy the selection
yy young (copy a line)
dd delete (cut a line)
p (paste)
P if you want to paste it above the current line
x to delete a single character.
dw to delete the rest of the word. from the current position of the cursor to the end of the word
D to delete from the current cursor position up to the end of the line
o open a new line under the current position of the cursor
O open a new line above the current position of the cursor.
r modify character. put the cursor on the letter and press r. Next type a new letter as a replacement for the letter .
e Moves to the end of a word
b Moves to the beginning of a word
$ Moves to the end of a line
H Moves to the first line onscreen
L Moves to the last line onscreen
a Appends after the cursor
o Opens a new line below and inserts
O Opens a new line above and inserts
R Enters insert mode but replaces characters instead of inserting
dd Deletes the current line
x Deletes text under the cursor
yy Yanks (copies) the current line
p Pastes the yanked line
u Undoes the last action

Last line mode

:set nonumber don’t show lines numbers
:set number show lines numbers
:number go to a line
:q to quit page
:q! to quit page without saving
:w to save page If you want to save it under a new filename, just enter the new name after the w command.
:wq to save and quit page
:/string search a string in file contains
:%s/oldtext/newtext/g replaces oldtext with newtext in the current file

Usual Commands



Usual Commands on files

[root@lab ~]# wc option FILE wc Provides a word or line count [+]


[root@lab ~]# wc -l /etc/passwd Count File Number of Lines


Eninam BAGNABANA     |         |     bagnadavid@outlook.fr     |