Linux has several tools for working with and transforming text files. These tools are called text filters and are useful for working with scripts, verifying log files, etc.
Use:
$ cat [options] file
When the options appear in brackets, they are actually optional.
The cat command concatenates files, prints your screen content and you can still receive text typed via the keyboard to a file.
Let’s see how to create a file with just a few lines of text:
$ cat > teste.txt
Now you can type any text. When you’re done, press Ctrl d on an empty line to end the input data and save the teste.txt file.
To view the content of the newly created file:
$ cat teste.txt
Cat can also be used to concatenate files.
$ cat texto1.txt > texto.txt
Observe that in this example the contents of the texto.txt file are replaced by texto1.txt.
To add the contents of the texto1.txt to the end of the texto.txt file The correct one would be:
$ cat texto1.txt >> texto.txt
Use:
$ cut file options
The cut command translated literally means cut. He reads the content of one or more files and is output from a vertical column.
Your most frequent options are:
Examples:
$ cut —d: -f 1 /etc/passwd
$ cut —b 1 /etc/passwd
$ cat /etc/hosts | cut —f 2
Use:
$ expand [options] file
The expand command changes the Tab (tab) within the texts to number of corresponding spaces. It is useful for making a text that makes use of more attractive tabs for certain devices, such as video, printer, files, etc.
The commonly used options are:
Example:
$ expand LEIAME.TXT
Unexpand command
Use:
$ unexpand [options] file
The unexpand command replaces the single space with TAB (tab) within the texts, at the beginning of the lines. It is the inverse of the expand command.
The commonly used options are:
Example:
$ unexpand LEIAME.TXT
Use:
$ fmt [options]
The fmt (format) command formats a text with a specific width. It can remove spaces or add spaces according to the desired width. The default is 75 characters.
The frequently used option is:
Example:
$ fmt —w 50 LEIAME.TXT
Use:
$ head [options]
The head command shows the first 10 lines at the beginning of the text.
The frequently used option is:
Example:
$ head —n 50 LEIAME.TXT
Use:
$ join [options] file1 file2
O The join command unites the lines of both files that have a common index. The join command can be used like a simple database.
The commonly used options are:
Example:
Assume that file1 contains the following content:
1 GZH-1234
2 HYD-2389
3 GIS-2348
And file 2 has the following content:
1 Fiat Uno Mille Smart
2 Audi A3
3 Monza
After the command:
$ join —j 1 file1 file2
The output will be as follows:
1 GZH-134 Fiat Uni Mille Smart
2 HYD-2389 Audi A3
3 GIS-2348 Monza
TABLE 5 – Symbols to differentiate header and footer
Symbol | Description |
\:\:\: | Symbol used to start the text header |
\:\: | Symbol used to start the body of the text |
\: | Symbol used to start the text footer |
Notice that the join command requires that both files contain an index, as in the example.
Use:
$ nl [options] [file]
The nl (number) command lines) is used to number the lines of a file. The command consider special conditions for the header and footer of the file.
The commonly used options are:
The sub-options are:
Example:
Assume that the arquivo.txt has the following content:
\:\:\: Grades and Attendance
Report for Software Engineering students
—————————————————————————————————————————————————————————————————————
———————————————————————————————————————
\:
Carlos Torres 8.5 80% Approved
José Antônio 10.0 100% Approved
Maria de Lourdes 10.0 100% & nbsp; Approved
Mário Cabral 9.5 100% Approved
\:
————————————————————————————————————————
And we use the nl command:
$ nl arquivo.txt
The result will be:
Grades and Attendance Report for Software Engineering students
————————————————————————————————————————————————————————
———————————————————————————————————
1 Carlos Torres 8.5 80% Approved
2 José Antônio & nbsp; 10.0 100% Approved
3 Maria de Lourdes 10.0 & nbsp; 100% Approved
4 Mário Cabral 9.5 100% Approved
————————————————————————————————— ————
Use:
$ od [options] [file]
O The od command (Octal and Other Formats) is used to view the content of a file in hexadecimal formats, octal, ASCII, and character names.
The frequently used option is:
Possible types are:
Example:
$ cat > arquivo.txt
Linux Certification
Ctrl d
$ od —t x arquivo.txt
0000000 74726543 63696669 6fe3e761 6e694c20
0000020 00007875
0000022
Use:
$ paste [options] file1 file2
The paste command is used to concatenate the rows of several files into vertical columns.
The commonly used options are:
Example:
Assume that file1 has the following content:
Lemmoraes
Rodrigues
Aduarte
And file 2 has the following content:
Provedor.com.br
provedor2.com.br
provider3.com.br
When using the paste command, the result will be as follows:
$ paste —d’@’ file1 file2
lmmoraes@provedor.com.br
rrodrigues@provedor2.com.br
aduarte@provedor3.com.br
Use:
$ pr [options] file
The pr (printing) command formats a text file for paginated output with defined header, margins, and width. It is useful for formatting raw texts for printing. The header consists of date, time, file name, and number of page.
The commonly used options are:
Example:
Use:
$ split [options] input_file output_file
The split command is used to split large files into smaller n-files. The output files are generated according to the number of lines in the input file.
The default is to split the file every 1000 lines. The names of the output files follow the standard filesaidaaa filesaidaab filesaidaac files, and so on.
The most common option is:
Example:
$ split -20 arquivo1.txt arquivosaida.txt
Use:
$ tail [options] file
The tail command displays the last 10 lines from a file. It works like the opposite of the head command.
The most common options are:
Example:
$ tail —n 50 /var/log/messages
$ tail —f /var/log/messages
Use:
$ tr [options] variable_search variable_swap
The tr command replaces one variable with another specified one. This command does not work directly with files, so it must be used with the standard output of another command.
The most common options are:
Example:
$ cat file1 | tr a-z A-Z
In this example, the tr command changes all the letters from a to z to capital letters.
$ cat file1 | tr —d a
In this example, the tr command erases the letter a.
$ cat file1 | tr —s 1
In this example, the tr command suppresses the repeated occurrences of number 1.
The tr command can swap characters from the search variable to the swap variable, but the number of characters must be the same in both.
Use:
$ wc [options] [files]
The wc command counts the lines, words, and characters of one or more files. If more than one file is passed as an argument, it will present the statistics for each file and also the total.
The most common options are:
Example:
$ toilet LEIAME.TXT
Use:
$ sort [-b] [-d] [-f] [-i] [-m] [-m] [-n] [-r] [-u] file [-o output_ file]
The sort command sorts the lines of a text file.
Your options are:
E.g.:
$ sort file
Use:
$ uniq [Options]… [Input_File [Output_File]]
The uniq command removes duplicate lines from an ordered file.
The most common options are:
Use:
$sed [Options] {script} file
The sed command is a powerful string editor for filtering or editing text sequences.
The most common options are:
Examples:
To replace In other words, we use the “s”, with the “/” delimiters, in such a way that the first occurrence is the text to be searched for, and the second is the text that will be replaced. Note that sed will only change the first occurrence of each line, and is case-sensitive. The default output of sed will be terminal.
$ cat file
will be hot at night today. The Night is beautiful.
$ sed s/night/day/ file
Today will be hot during the day. The Night is beautiful.
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.