Shell in text processing tools

1.grep

Linux systems grep command is a powerful text search tool, it can use a regular expression search text, and the matching line printed. grep stands for Global Regular Expression Print, represents the global regular expression version, its usage rights for all users.
Tips: grep -E = egrep (extended regular expressions)
contents of the file:
Here Insert Picture Description

## grep format ##
grep matching condition with files

grep root passwd               过滤root关键字

Here Insert Picture Description

grep ^root passwd               以root开头

Here Insert Picture Description

grep root$ passwd               以root结尾

Here Insert Picture Description

grep -i root passwd                忽略大小写

Here Insert Picture Description

grep -E "\<root" passwd            root字符之前不能有字符

Here Insert Picture Description

grep -E "root\>" passwd            root字符之后不能有字符

Special symbols are extended regular expressions, it should be added -E parameter
Here Insert Picture Description
roothaha disappeared.

grep -数字               显示过滤行以及上面几行和下面几行,A为文件名。

Here Insert Picture Description

grep -n                   显示匹配的行所在行号

Here Insert Picture Description

grep -A                   显示过滤行以及下面几行
grep -B                   显示过滤行以及上面几行

Here Insert Picture Description

grep -v                    反向过滤

Here Insert Picture Description
grep number of characters matching rules
Here Insert Picture Description

^westos                     以westos开头
westos$                     以westos结尾

Here Insert Picture Description

w....s                     w开头s结尾中间4个任意字符

Here Insert Picture Description

.....s                      s结尾前面5个任意字符

Here Insert Picture Description

*                       字符出现0到任意次

Here Insert Picture Description

?                       0到1次

Here Insert Picture Description

+                       1到任意次

Here Insert Picture Description

{n}                       n次

Here Insert Picture Description

{m,n}                      m到n次

Here Insert Picture Description

{0,n}                      0-n次

Here Insert Picture Description

{,n}                       0-n次

Here Insert Picture Description

{m,}                      最少m次

Here Insert Picture Description

(lee){2}                     lee字符串出现2次,lee要用括号括起来

Here Insert Picture Description

2.sed

sed stream editor is an abbreviation of a stream editor, which processes a row contents. Handling, storing the row currently being processed in a temporary buffer, called a "model space" (pattern space), followed by treatment with the contents of the buffer sed command, the processing is completed, the contents of the buffer sent to the screen. Then the next line, which is repeated until the end of the file. File contents not changed, unless you use redirection to store the output. sed primarily used to automatically edit one or more files, the file operation is repeated to simplify the preparation of the conversion procedures.

Format:

sed 参数 命令 处理对象

Here Insert Picture Description
## - b display line numbers, you can see only the contents of the fifth row are printed out.

sed 参数 处理对象 -f 处理规则文件

Here Insert Picture Description

Handling of character

-n ## silent mode, only the line display process.
-e number of policy #.
p ## print, display print

sed -n 5p westos                  显示第五行

Here Insert Picture Description

sed -n 3,5p westos                 显示3到5行

Here Insert Picture Description

sed -ne "3p;5p" westos                显示3和5行

Here Insert Picture Description

sed -ne 1,5p westos                 1-5行

Here Insert Picture Description

sed -ne '5,\$p' westos                5到最后以行

Here Insert Picture Description

sed -n '/^#/p' fstab                  显示以#开头的行

Here Insert Picture Description


d delete

sed 5d westos                 删除第五行

Here Insert Picture Description

sed  '/^#/d'  fstab               把 # 开头的行删除

Here Insert Picture Description

sed  '/^UUID/!d'  fstab             除了UUID以外的行都删除

Here Insert Picture Description

sed -e '5,$d' westos              第五行删到最后

Here Insert Picture Description


Add a

sed  -e  '\$a hello world'  westos

Here Insert Picture Description

sed -e  '$a hello\nworld'  westos    \n换行

Here Insert Picture Description

sed  -e '/^#/a hello world' fstab

Here Insert Picture Description


c Replace

sed -e '/^#/c hello world' fstab                    以'#'开头的行都替换为 helloworld

Here Insert Picture Description

sed '5c hello world' westos

Here Insert Picture Description


w writes the rows that meet the specified file

sed '/^UUID/w westofile' westos        把westos中UUID开头的行写入westosfile中

Here Insert Picture Description

i Insert

sed '5ihello westos' westos

Here Insert Picture Description
Below is inserted into a, i is inserted to the upper


r consolidate file

sed  '5r hah'  westos            把hah文件的内容整合到westos文件第五行下面

Here Insert Picture Description

sed character replacement

sed 's/: /###/g' westos         把全文的: 替换为'###'

Here Insert Picture Description

sed 's/: /###/' westos            仅替换第一列

Here Insert Picture Description

sed '1,5s/: /###/g' westos                1到5行

Here Insert Picture Description

sed '1s/: /###/g' westos          替换第一行

Here Insert Picture Description

sed '1s/: /###/g;5s/: /###/g' westos        替换第一行和第五行

Here Insert Picture Description

sed '/lp/,/shutdown/s/: ###/g' westos       替换lp 和shutdown之间的:

Here Insert Picture Description

sed 's/\//####/g' westos        替换/为###

Here Insert Picture Description

sed 's@/@####@g' westos            @的作用和/一样

Here Insert Picture Description

sed 's@/@####@g' -i westos                    把sed处理的内容保存到westos文件中

Here Insert Picture Description
Visible content processing has been changed to the source file.

3.awk

Published 36 original articles · won praise 13 · views 1436

Guess you like

Origin blog.csdn.net/thermal_life/article/details/105118036
Recommended