The basic commands of linux (4)

12. printf
printf'output type output format' output content
output type:
%ns: output string. n is a number that refers to the output of several characters
%ni: output integer. n is a number that refers to output several numbers
%m.nf: output floating point numbers. m and n are numbers, referring to the number of integer
digits and decimal digits of the output . For example, %8.2f represents a total of 8 digits output, of
which 2 digits are decimals and 6 digits are integers.

13. awk@
#awk'Condition 1{Action 1} Condition 2{Action 2}...' File name
Condition (Pattern):
Generally use relational expressions as the condition
x> 10 to determine whether the variable x is greater than 10
x>=10 is greater than Equal to
x<=10 Less than or equal to
Action:
Formatted output
Flow control statement

#awk ‘{printf $2 “\t” $6 “\n”}’ student.txt
#df -h | awk ‘{print $1 “\t” $3}’
#awk ‘BEGIN{printf “This is a transcript \n” }
{printf $2 “\t” $6 “\n”}’ student.txt
#awk ‘END{printf “The End \n” }
{printf $2 “\t” $6 “\n”}’ student.txt
指定分隔符
#cat /etc/passwd | grep “/bin/bash” |
awk ‘BEGIN {FS=":"} {printf $1 “\t” $3 “\n”}’
#cat student.txt | grep -v Name |
awk ‘$6 >= 87 {printf $2 “\n” }’

Fourteen, sed command@

The sed command
sed is a lightweight stream editor included in almost all UNIX platforms (including Linux). sed is mainly used to select, replace, delete, and add data.

[root@localhost ~]# sed [options]'[action]' file name
options:
-n: general sed command will output all data to the screen, if you add this option, only the lines processed by sed command Output to the screen.
-e: Allow multiple sed commands to edit the input data
-i: use the modified result of sed to directly modify the file that reads the data, instead of outputting the data on the screen

如:
df -h|awk ‘{printf $2 “\n”}’|sed -n “2,4p”
df -h|awk ‘{printf $2 “\n”}’|sed -n “/2/p”

Action:
a \: Append, add one or more lines after the current line. When adding multiple rows, except for the last row, the end of each row needs to use "\" to indicate that the data is not completed.
c \: Line replacement, replace the original data line with the character string after c. When replacing multiple lines, except for the last line, the end of each line needs to use "\" to indicate that the data is incomplete.
i \: Insert, insert one or more rows before the current row. When inserting multiple rows, except for the last row, the end of each row needs to use "\" to indicate that the data is not completed.
d: Delete, delete the specified row.
p: Print, output the specified line.
s: string replacement, replace one string with another string. The format is "line range s/old string/new string/g" (similar to the replacement format in vim).

sed ‘/test/r file’ filename
sed -n ‘/test/w file’ example
sed -i ‘2a\this is a test line’ test.conf
sed ‘/^test/i\this is a test line’ file
sed -e ‘1,5d’ -e ‘s/test/check/’ file
sed ‘/test/{ n; s/aa/bb/; }’ file

15. Sorting command sort@

1. Sorting command sort
[root@localhost ~]# sort [options] File name
Options:
-f: Ignore case
-n: Sort by number, use string sort by default
-r: Reverse sort
-t: Specify the separator. The default separator is a tab character
-kn[,m]: Sort according to the specified field range. Start from the nth field and end in the m field (default to the end of the line)

Guess you like

Origin blog.csdn.net/qq_34134299/article/details/109117816