Linux string interception and processing commands (cut, printf, awk, sed, sort, wc) Notes
Article Directory
1.cut command
cut [options] filename
-f 列号 #提取第几列(分隔符默认为\t)
-d 分隔符 #指定分隔符
For example: cut -f 2 a.txt #Intercept the second column of the content of the file a.txt (column number starts from 1)
cut -f 2,4 a.txt #Intercept the second and fourth columns of the content of the file a.txt Column
cut -d ":" -f 1,3 /etc/passwd #Intercept the first column and the third column of the passwd file to: divide the first and third column
2. printf command
printf'output type output format' output content
output type:
%ns 输出字符串,n是数字,指代输出几个字符
%ni 输出整数。n是数字,指代输出几个数字
%m.nf 位数和小数位数。例如:%8.2f 代表输出8位数,其中2位是小数,6位是整数
输出格式:
\a 输出警告声音
\b 输出退格键,也就是BackSpace键
\f 消除屏幕
\n 换行
\r 回撤
\t 水平制表符
\v 垂直制表符
For example:
printf'%s %s %s\n' 1 2 3 4 5 6 # means every three digits are output in a group, and a newline character is added
printf'%s\t%s\t%s\t' $( cat a.txt) # means output in 4 columns
3. The awk command
awk.'Condition 1{Action 1}Condition 2{Action 2}...' File name
awk'{printf $2 “\t” $6 “\n”}' a.txt #The second column and the first column of the output file a.txt 6 columns
4. sed command
sed [options]'[action]' filename# Description: Action must be enclosed in quotation marks
Options:
-n 一般sed命令会把所有数据都输出到屏幕。如果加入此选择,则只会把经过sed命令处理的行输出到屏幕。
-e 允许对输入数据应用多条sed命令编辑
-i 用sed的修改结果直接修改读取的数据的文件,而不是修改屏幕输出
动作:
a\ 追加,在当前行后添加一行或多行。添加多行时除最后一行外,每行末尾需要用"\"代表数据未完结。
c\ 行替换,用c后面的字符替换原数据行,替换多行时除最后一行外,每行末尾需要用"\"代表数据未完结。
i\ 插入,在当前插入一行或多行,插入多行时,除最后一行外每行末尾需用"\"代表数据未完结。
d 删除,删除指定的行
p 打印,输出指定的行
s 字符串替换,用一个字符串替换另外一个字符串。格式为"行范围 s/旧字符串/新字符串/g"
sed可以接收管道符的输出结果
For example:
sed '2p' a.txt #输出第2行后,又把所有内容输出一遍
sed -n '2p' a.txt #只输出第二行
sed '2,4d' a.txt #删除第2到4行,只删除屏幕输出,不会更改文件本身的内容
sed '2a hello' a.txt #在第二行插入一行 hello
sed '2i hello' \
word' a.txt #在第二行前插入多行 hello 一行 word一行
sed '2c no person' a.txt #用no person 替换第二行
sed '4s/99/55/g' a.txt #把第4行的99替换为55
sed -i '4s/99/55/g' a.txt #把第4行的99替换为55,修改的是原文件而不是屏幕输出
sed -i 's/99/55/g' a.txt #s前不加行号时表示替换整个文件中匹配的字符串
sed -e 's/Liming//g;s/Gao//g' a.txt #-e表示允许多个条件执行,把Liming替换为空,把Gao替换为空
5. The sort command
sort [option] file name#sort
option:
-f 忽略大小写
-n 以数值型进行排序,默认使用字符串型排序
-r 反向排序
-t 指定分隔符,默认分隔符是制表符
-k n[,m] 按照指定的字段范围排序。从第n字段开始,m字段结束(默认到行尾)
6. wc command
wc [options] file name#statistics
options:
-l 只统计行数
-w 只统计单词数
-m 只统计字符数
7. Linux string interception command
Suppose there is a variable var=http://www.google.com/test.htm
1. # interception, delete the left character, and keep the right character.
echo ${var# //}
where var is the name of the variable, # is the operator, // means to delete the first // number and all the characters on the left from the left,
ie delete http://, the
result is: www.google .com/test.htm
2. ## is intercepted, the left character is deleted, and the right character is retained.
echo ${var## /}
## / means to delete the last (far right) / sign and all the characters on the left from the left,
ie delete http://www.google.com/ and the
result is test.htm
3. The% sign is intercepted, the characters on the right are deleted, and the characters on the left are retained.
echo ${var%/ }
%/ means that starting from the right, delete the first / sign and the characters on the right. The
result is: http://www.google.com
4. Intercept the %% sign, delete the right character, keep the left character
echo ${var%%/ }
%%/ means start from the right, delete the last (leftmost) / sign and the right character The
result is: http:
5. Starting from the first character on the left, and the number of characters
echo ${var:0:5},
where 0 means starting from the first character on the left, and 5 means the total number of characters.
The result is: http:
6. Start with the first few characters from the left and continue to the end.
The 7 in echo ${var:7}
means that the 8th character from the left starts and continues to the end.
The result is: www.google.com/test.htm
7. Starting from the first few characters from the right, and the number of characters
echo ${var:0-7:3}
where 0-7 means the seventh character from the right, and 3 means the number of characters.
The result is: test
8. Start with the first few characters from the right and continue to the end.
echo ${var:0-7}
means starting from the seventh character from the right and continuing to the end.
The result is: test.htm
Note: (The first character on the left is represented by 0, and the first character on the right is represented by 0-1)