Shell tools


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 changedUnless 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)

-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  //Separate to sort in the second column

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]