1、 Cut
Cut is “cut”, which is used to cut data in a file. The cut command cuts bytes, characters and fields from each line of the file and outputs these bytes, characters and fields.
1、Basic grammar
Cut [option parameter] filename // the default separator is tab
Parameter Description:
-F column number, which column to extract
-D separator, which splits the column according to the specified separator
aaa bbb
ccc ddd
eee fff
cut -d " " -f 1 test.txt
2、 Sed
Sed is a stream editor that processes one line at a time. When processing, the current line is stored in the temporary buffer, which is called “mode space”. Then the SED command processes the contents of the buffer. After processing, the contents of the buffer are sent to the screen. Then process the next line, repeating it until the end of the file.The contents of the document have not changed
Unless you use redirection to store output.
1、Basic grammar
Sed [option parameter]'command 'file name
Description of option parameters:
-E. edit sed action directly in instruction line mode
Command function description:
A is added. A string can be followed by a, and a quotation mark appears in the next line
D delete
S find and replace
aaa bbb
ccc ddd
eee fff
sed "2a ttt sss" test.txt //Add a line under the second line
aaa bbb
ccc ddd
ttt sss
eee fff
3、 Awk
Awk is a very powerful text analysis tool, which reads the file line by line, slices each line with space as the default separator, and then analyzes the cut part.
1、Basic grammar
Awk [option parameter]'pattern1 {action1} pattern2 {action2}... 'file name
Pattern: indicates what awk looks for in the data, which is the matching pattern
Action: a list of commands to execute when a match is found
Description of option parameters:
-F specifies the input file break
-V assign a user-defined variable
Awk built-in variable:
File name
NR number of records read
Number of fields of NF browsing records (number of columns after cutting)
//Searches all rows that start with root and outputs the seventh column of that row
awk -F : '/^root/{print $7}' /etc/passwd
4、 Sort
Awk sorts the files and outputs the sorting results in standard.
1、Basic grammar
Sort (option) (parameter)
Options:
-N is sorted by value
-R is sorted in reverse order
-T sets the separator character used for sorting
-K specifies the columns to be sorted
Parameter: Specifies the file list to be sorted
sort -t : -nrk 2 test.sh //Separate to sort in the second column