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 arg1arg2file 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@lab ~]# grep varfile only lines which contains var view
Matching Using Symbols
[root@lab ~]# grep ^varfile 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
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.
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.
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 .
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
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.
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 optionsfile
■ 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 optionFILE wc Provides a word or line count
[+]
[root@lab ~]# wc -l /etc/passwd Count File Number of Lines