Linux common text processing tools

Copyright: original works, posted please indicate the source! https://blog.csdn.net/sa19861211/article/details/90341892

A, sort command

sort command sorts the File parameter specifies the file, and writes the result to standard output.
If the File parameter specifies more than one file, then the sort command will connect these documents, and as a file sort.

语法:
sort [-fbMnrtuk] [file or stdin]

Options and parameters:
-f: ignore case differences, a and A are treated the same
-b: Ignore the front spaces
-M: the name of the month to be sorted, such as JAN, DEC and other sorting methods
-n: Use pure numbers sorting (default text is sorted lexicographically)
-R & lt: reverse sort
-t: separator, default is to split the Tab key
-u: deduplication
-k: use what sort columns (column 1 defaults put in order)

Example:

Examples Example shows
cat /etc/passwd |sort Username to / etc / passwd file in ascending order
cat /etc/passwd |sort -t “:” -k 3 Of / etc / passwd file by using a third column in ascending order
cat /etc/passwd |sort -t “:” -k 3n Of / etc / passwd file using the first three sort ascending numerical order
cat /etc/passwd |sort -t “:” -k 3nr Of / etc / passwd file using the numerical order of three in descending order
cat /etc/passwd |sort -t “:” -k 6.2,6.4 -k 1r Of / etc / passwd file, first sorted in ascending order with the first 2-4 characters of 6, and then to the first column in descending order
cat /etc/passwd |sort -t “:” -k 7 -u Of / etc / passwd file with 7 column in ascending order and deduplication

Two, uniq command

uniq removes the sorted file duplicates. Thus uniq sort often used in combination, in order to make uniq function, must be repeated adjacent row.

Syntax:
uniq [-icu]

Options and parameters:
-i: ignore case
-c: technical
-u: show only a single line (default when no arguments)

Example:

Examples Example shows
cat testfile | sort | uniq Testfile going to be heavy (sort must first go before the weight)
cat testfile | sort | uniq -c Repetitions statistics testfile file line

Three, cut command

cut command extracts the text from a text file or text column flow.
Example:

Examples Example shows
wc -c test | cut -d ’ ’ -f 1 In '' is a delimiter, taking only the first column
echo $PATH | cut -d ‘:’ -f 3,5 In ':' separator is, take the first three and five
echo $PATH | cut -d ‘:’ -f 3-5 In ':' separator is taken 3-5
echo $PATH | cut -d ‘:’ -f 3-5,7 In ':' separator is taken 3-5 and column 7
echo $PATH | cut -d ‘:’ -f 3- In ':' separator is, take the last 3 to a

Four, sed command

sed is a good file processing tool itself is a command pipeline, mainly in units of processing, data lines can be replaced, delete, add, and other select specific job.

Syntax:
sed [-nefri] [-e

Parameter Description:
-n: using the quiet (silent) mode. Sed in general usage, all of the content from the standard input will be output to the screen. If the -n argument together, only the output line through sed processed.
-e

Action Description:
a: added, followed by a string may be connected, and these strings appear (this next line) in a new line;
c: substituted, may be connected to the back c string, these strings can substituted n1, the line between N2;
d: deleted, because it is deleted ah, so are not normally connected to any later d pat;
i: inserted behind i may take a string, the string which appears on a new line (the current line on one line);
p: print, that will be a selection of printed information. P will operate normally with the parameters sed -n together;
s: replacement, will replace the specific string into a new string! Usually this s action can be used with regular expressions! For example 1,20s / old / new / g is it!

Examples :( Suppose we have a file called ab)
to delete the line:

Examples Example shows
sed ‘1d’ testfile Delete the first line of the file testfile
sed ‘1,3d’ testfile Delete the file testfile line 1 to line 3
sed ‘$d’ testfile Delete the last line of the file testfile
sed ‘3,$d’ testfile Delete the file testfile the third line to the last line
sed '/test/'d testfile According to delete mode, delete the file testfile contains all lines of test

Display a specific line

Examples Example shows
sed -n ‘1p’ testfile Display line 1
sed -n ‘$p’ testfile The last line
sed -n ‘1,3p’ testfile Display line 1 to line 3
sed -n ‘3,$p’ testfile Display 3rd line to the last line

Use query mode

Examples Example shows
sed -n ‘/ruby/p’ testfile Query all the rows that contain the keyword of ruby
sed -n ‘/\$/p’ testfile $ Query includes the keyword where all the lines, you must use the backslash \ to escape special characters $

Insert Rows

Examples Example shows
sed ‘1a drink tea’ testfile After the increase in the first line character string "drink tea"
sed ‘1,3a drink tea’ testfile Line 1 to line 3 increases after the character string "drink tea"
sed -i ‘$a bye’ testfile The last line insert "bye" in the testfile file
sed ‘1a drink tea\nor coffee’ testfile After row 1 line 2 increases, the intermediate must newline \ n divided

Replace the entire row

Examples Example shows
sed ‘1c Hi’ testfile Line 1 in place of the Hi
sed ‘1,3c Hi’ testfile Line 1 to line 3 in place of the Hi

Replacing a portion of the line
syntax:
Sed 'S / string to replace / new string / g' (string can be replaced with a regular expression)

Examples Example shows
sed ‘s/ruby/bird/g’ testfile Replace bird string ruby
sed -n ‘/ruby/p’ testfile | sed ‘s/ruby/bird/g’ Replace bird string ruby
sed -n ‘/ruby/p’ testfile | sed ‘s/ruby//g’ Delete ruby ​​string in each row
sed -n ‘s/^127.0.0.1/&localhost/’ testfile Add string behind 127.0.0.1 localhost
sed -n ‘s/(love)able/\1rs/’ testfile grouping able to love, a packet is later replaced rs

Five, awk command

awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息。
awk处理过程: 依次对每一行进行处理,然后输出。
awk命令形式:

awk [-F|-f|-v] ‘BEGIN{} //{command1; command2} END{}’ file

参数说明:
–F:指定分隔符
-f:调用脚本
-v:定义变量 var=value
’ ’ :引用代码块
BEGIN 初始化代码块,在对每一行进行处理之前,初始化代码,主要是引用全局变量,设置FS分隔符
// :匹配代码块,可以是字符串或正则表达式
{} :命令代码块,包含一条或多条命令
;:多条命令使用分号分隔
END 结尾代码块,在对每一行进行处理之后再执行的代码块,主要是进行最终计算或输出结尾摘要信息。

特殊要点:

$0 表示整个当前行
$1 每行第一个字段
NF 字段数量变量
NR 每行的记录号,多文件记录递增
FNR 与NR类似,不过多文件记录不递增,每个文件都从1开始
\t 制表符
\n 换行符
FS BEGIN时定义分隔符
RS 输入的记录分隔符, 默认为换行符(即文本是按一行一行输入)
~ 匹配,与==相比不是精确比较
!~ 不匹配,不精确比较
== 等于,必须全部相等,精确比较
!= 不等于,精确比较
&& 逻辑与
|| 逻辑或
+ 匹配时表示1个或1个以上
/[0-9][0-9]+/ 两个或两个以上数字
/[0-9][0-9]*/ 一个或一个以上数字
FILENAME 文件名
OFS 输出字段分隔符, 默认也是空格,可以改为制表符等
ORS 输出的记录分隔符,默认为换行符,即处理结果也是一行一行输出到屏幕
-F’[:#/]’ 定义三个分隔符 冒号(:)、井号( # )和斜杠( /)

print:是awk打印指定内容的主要命令。

命令 说明
awk ‘{print}’ /etc/passwd 或 awk ‘{print $0}’ /etc/passwd 逐行输出/etc/passwd文件的内容
awk '{if($3>30000){print $1" “$2” “$3”}} tomcat.log 过滤出第四列>30000的行,并只打印2、3、4列
awk ‘{print " "}’ /etc/passwd 不输出/etc/passwd文件的内容,而是输出相同行数的空行
awk ‘{print “a”}’ /etc/passwd 不输出/etc/passwd文件的内容,而是输出相同行数的a行,每行就只有一个字母a
awk -F":" ‘{print $1}’ /etc/passwd 只输出第一列
awk -F: ‘{print $1; print $2}’ /etc/passwd 输出第1列和第2列
awk -F: ‘{print $1,$3,$6}’ OFS="\t" /etc/passwd 输出1,3,6列,并以制表符作为分隔符

-f:指定脚本文件

awk -f script.awk  file
BEGIN{
FS=":"
}
{print $1}    

说明:效果与awk -F":" '{print $1}'相同,只是分隔符使用FS在代码自身中指定

awk 'BEGIN{X=0} /^$/{ X+=1 } END{print "I find",X,"blank lines."}' test 

结果:I find 4 blank lines.

ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is",sum}'      

计算文件大小:total size is 17487

-F指定分隔符
$1 是按照指定分隔符分割后的第一个字段,$3指第三个字段。
\t是制表符,一个或多个连续的空格或制表符看做一个定界符,即多个空格看做一个空格。

示例:

命令 说明
awk -F":" ‘{print $1}’ /etc/passwd 输出$1
awk -F":" ‘{print $1 $3}’ /etc/passwd $1与$3相连输出,不分隔
awk -F":" ‘{print $1,$3}’ /etc/passwd 多了一个逗号,$1与$3使用空格分隔
awk -F":" ‘{print $1 " " $3}’ /etc/passwd $1与$3之间手动添加空格分隔
awk -F":" ‘{print “Username:” $1 “\t\t Uid:” $3 }’ /etc/passwd 自定义输出
awk -F: ‘{print NF}’ /etc/passwd 显示每行有多少字段
awk -F: ‘{print $NF}’ /etc/passwd 将每行第NF个字段的值打印出来
awk -F: ‘NF==4 {print }’ /etc/passwd 显示只有4个字段的行
awk -F: ‘NF>2{print $0}’ /etc/passwd 显示每行字段数量大于2的行
awk ‘{print NR,$0}’ /etc/passwd 在每行内容前面添加行号
awk -F: ‘{print NR,NF,$NF,"\t",$0}’ /etc/passwd 依次打印行号,字段数,最后字段值,制表符,每行内容
awk -F: ‘NR==5{print}’ /etc/passwd 显示第5行
awk -F: ‘NR==5 || NR==6{print}’ /etc/passwd 显示第5行和第6行
route -n|awk ‘NR!=1{print}’ 不显示第一行

匹配代码块
//:纯字符匹配
!//:纯字符不匹配
~//:字段值匹配
!~//:字段值不匹配
~/a1|a2/字段值匹配a1或a2

示例:

命令 说明
awk ‘/mysql/’ /etc/passwd 输出匹配mysql的行
awk ‘/mysql/{print }’ /etc/passwd 输出匹配mysql的行
awk ‘/mysql/{print $0}’ /etc/passwd 输出匹配mysql的行
awk ‘!/mysq\l/{print $0}’ /etc/passwd 输出不匹配mysql的行
awk ‘/mysql|mail/{print}’ /etc/passwd 输出匹配mysql或mail的行
awk ‘!/mysql|mail/{print}’ /etc/passwd 输出不匹配mysql和mail的行
awk -F: ‘/mail/,/mysql/{print}’ /etc/passwd 区间匹配,输出匹配mail和mysql之间的字符串的行
awk ‘/[2][7][7]*/{print $0}’ /etc/passwd 匹配包含27为数字开头的行,如27,277,2777…
awk -F: ‘$1~/mail/{print $1}’ /etc/passwd $1匹配指定内容才显示
awk -F: ‘{if($1~/mail/) print $1}’ /etc/passwd 与上面相同
awk -F: ‘$1!~/mail/{print $1}’ /etc/passwd $1不匹配mail时才显示
awk -F: '$1!~/mail mysql/{print $1}’ /etc/passwd

IF语句
必须用在{}中,且比较内容用()扩起来。

示例:

命令 说明
awk -F: ‘{if($1~/mail/) print $1}’ /etc/passwd 简写
awk -F: ‘{if($1~/mail/) {print $1}}’ /etc/passwd 全写
awk -F: ‘{if($1~/mail/) {print $1} else {print $2}}’ /etc/passwd if…else…

条件表达式
==:等于
!=:不等于
> :大于
>= :大于
示例:

命令 说明
awk -F":" ‘$1==“mysql”{print $3}’ /etc/passwd 等于
awk -F":" ‘{if($1==“mysql”) print $3}’ /etc/passwd 同上
awk -F":" ‘$1!=“mysql”{print $3}’ /etc/passwd 不等于
awk -F":" ‘$3>1000{print $3}’ /etc/passwd 大于
awk -F":" ‘$3>=100{print $3}’ /etc/passwd 大于等于
awk -F":" ‘$3<1{print $3}’ /etc/passwd 小于
awk -F":" ‘$3<=1{print $3}’ /etc/passwd 小于等于

逻辑运算符
&& 和 ||

示例:

命令 说明
awk -F: ‘$1~/mail/ && $3>8 {print }’ /etc/passwd 逻辑与,$1匹配mail,并且$3>8
awk -F: ‘{if($1~/mail/ && $3>8) print }’ /etc/passwd 同上
awk -F: ‘$1~/mail/ || $3>1000 {print }’ /etc/passwd 逻辑或
awk -F: ‘{if($1~/mail/ || $3>1000) print }’ /etc/passwd 同上

数值运算
示例:

命令 说明
awk -F: ‘$3 > 100’ /etc/passwd 输出’$3 > 100的行
awk -F: ‘$3 > 100 || $3 < 5’ /etc/passwd 输出’$3 > 100或$3 < 5的行
awk -F: ‘$3+$4 > 200’ /etc/passwd 输出$3+$4 > 200的行
awk -F: ‘/mysql|mail/{print $3+10}’ /etc/passwd 第三个字段加10打印
awk -F: ‘/mysql/{print $3-$4}’ /etc/passwd 减法
awk -F: ‘/mysql/{print $3*$4}’ /etc/passwd 求乘积
awk ‘/MemFree/{print $2/1024}’ /proc/meminfo 除法
awk ‘/MemFree/{print int($2/1024)}’ /proc/meminfo 取整

输出分隔符OFS

示例:

命令 说明
awk ‘$6 ~ /FIN/ || NR==1 {print NR,$4,$5,$6}’ OFS="\t" netstat.txt
awk ‘$6 ~ /WAIT/ || NR==1 {print NR,$4,$5,$6}’ OFS="\t" netstat.txt 输出字段6匹配WAIT的行,其中输出每行行号,字段4,5,6,并使用制表符分割字段

输出处理结果到文件

示例:

命令 说明
route -n|awk ‘NR!=1{print > “./fs”}’ 在命令代码块中直接输出
route -n|awk ‘NR!=1{print}’ > ./fs 使用重定向进行输出

格式化输出

netstat -anp\|awk '{printf "%-8s %-8s %-10s\n",$1,$2,$3}' 

printf:表示格式输出
%:格式化输出分隔符
-8:长度为8个字符
s:表示字符串类型
打印每行前三个字段,指定第一个字段输出字符串类型(长度为8),第二个字段输出字符串类型(长度为8),第三个字段输出字符串类型(长度为10)

netstat -anp|awk '$6=="LISTEN" || NR==1 {printf "%-10s %-10s %-10s \n",$1,$2,$3}'
netstat -anp|awk '$6=="LISTEN" || NR==1 {printf "%-3s %-10s %-10s %-10s \n",NR,$1,$2,$3}'

IF语句

awk -F: ‘{if($3>100) print “large”; else print “small”}’ /etc/passwd
awk -F: ‘BEGIN{A=0;B=0} {if($3>100) {A++; print “large”} else {B++; print “small”}} END{print A,"\t",B}’ /etc/passwd //ID大于100,A加1,否则B加1
awk -F: ‘{if($3<100) next; else print}’ /etc/passwd //小于100跳过,否则显示
awk -F: ‘BEGIN{i=1} {if(i<NF) print NR,NF,i++ }’ /etc/passwd
awk -F: ‘BEGIN{i=1} {if(i<NF) {print NR,NF} i++ }’ /etc/passwd
另一种形式
awk -F: ‘{print ($3>100 ? “yes”:“no”)}’ /etc/passwd
awk -F: ‘{print ($3>100 ? $3":\tyes":$3":\tno")}’ /etc/passwd

while语句

awk -F: 'BEGIN{i=1} {while(i<NF) print NF,$i,i++}' /etc/passwd 

结果:
7 root 1
7 x 2
7 0 3
7 0 4
7 root 5
7 /root 6

数组

netstat -anp|awk 'NR!=1{a[$6]++} END{for (i in a) print i,"\t",a[i]}'
netstat -anp|awk 'NR!=1{a[$6]++} END{for (i in a) printf "%-20s %-10s %-5s \n", i,"\t",a[i]}'

结果:
9523 1
9929 1
LISTEN 6
7903 1
3038/cupsd 1
7913 1
10837 1
9833 1

应用1:
awk -F: ‘{print NF}’ helloworld.sh //输出文件每行有多少字段
awk -F: ‘{print $1,$2,$3,$4,$5}’ helloworld.sh //输出前5个字段
awk -F: ‘{print $1,$2,$3,$4,$5}’ OFS=’\t’ helloworld.sh //输出前5个字段并使用制表符分隔输出
awk -F: ‘{print NR,$1,$2,$3,$4,$5}’ OFS=’\t’ helloworld.sh //制表符分隔输出前5个字段,并打印行号

应用2:
awk -F’[:#]’ ‘{print NF}’ helloworld.sh //指定多个分隔符: #,输出每行多少字段
awk -F’[:#]’ ‘{print $1,$2,$3,$4,$5,$6,$7}’ OFS=’\t’ helloworld.sh //制表符分隔输出多字段

应用3:
awk -F’[:#/]’ ‘{print NF}’ helloworld.sh //指定三个分隔符,并输出每行字段数
awk -F’[:#/]’ ‘{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12}’ helloworld.sh //制表符分隔输出多字段

应用4:
计算/home目录下,普通文件的大小,使用KB作为单位

 ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is:",sum/1024,"KB"}'

计算/home目录下,普通文件的大小,使用KB作为单位,int是取整的意思

ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is:",int(sum/1024),"KB"}'   

应用5:
统计netstat -anp 状态为LISTEN和CONNECT的连接数量分别是多少

netstat -anp|awk '$6~/LISTEN|CONNECTED/{sum[$6]++} END{for (i in sum) printf "%-10s %-6s %-3s \n", i," ",sum[i]}'

应用6:
统计/home目录下不同用户的普通文件的总数是多少?

ls -l|awk 'NR!=1 && !/^d/{sum[$3]++} END{for (i in sum) printf "%-6s %-5s %-3s \n",i," ",sum[i]}'   

结果:
mysql 199
root 374

统计/home目录下不同用户的普通文件的大小总size是多少?

ls -l|awk 'NR!=1 && !/^d/{sum[$3]+=$5} END{for (i in sum) printf "%-6s %-5s %-3s %-2s \n",i," ",sum[i]/1024/1024,"MB"}'

应用7:
输出成绩表

awk 'BEGIN{math=0;eng=0;com=0;printf "Lineno.   Name    No.    Math   English   Computer    Total\n";printf "------------------------------------------------------------\n"}{math+=$3; eng+=$4; com+=$5;printf "%-8s %-7s %-7s %-7s %-9s %-10s %-7s \n",NR,$1,$2,$3,$4,$5,$3+$4+$5} END{printf "------------------------------------------------------------\n";printf "%-24s %-7s %-9s %-20s \n","Total:",math,eng,com;printf "%-24s %-7s %-9s %-20s \n","Avg:",math/NR,eng/NR,com/NR}' test0

结果:
cat test0
Marry 2143 78 84 77
Jack 2321 66 78 45
Tom 2122 48 77 71
Mike 2537 87 97 95
Bob 2415 40 57 62

六、wc命令

输出每个指定文件的行数、单词计数和字节数。如果指定了多个文件,则输出所有文件的总计,如果没有指定文件或指定文件为“-”,则从标准输入读取数据。

Guess you like

Origin blog.csdn.net/sa19861211/article/details/90341892