shell entry - text processing Three Musketeers (grep, sed, awk)

Figure Three Musketeers operation after learning all replenish

1. grep

Global search regular expression and print out the line
comprehensive study of regular expression search and display the
grep command is a powerful text search tools, according to "die a user-specified
line" of the target text matching check, print match to
the regular expressions and basic text character or characters written filters

grep 的格式  
grep 匹配条件 处理文件  
例如 
grep root passwd 
grep ^root passwd 
grep root$ passwd 
grep -i root passwd 
grep -E "root|ROOT" passwd

The grep regular expressions

^westos
westos$ 
'w....s' 
'w.....' 
'.....s'

Grep matching the number of characters set

  • Character appears [0- any number of times]
    character appears [0-1]?
    + Character appears [1- ARBITRARY]
    characters appear {n} [n times]
    | {m, n-character appears} [happened appear m times, most appears n times]
    {0, n} character appears [0-n times]
    {m, the characters appear} [at least m]
    (XY) XY keyword appears {n} [n times]
    . * keyword matching between any character

Position matches the character set grep

^ Keyword
Keyword $
<keyword
keyword>
<keyword>

grep regular expressions and extended regular expression
regular grep does not support extended regular expression sub-vertical lines is used to mean "
or" extended regular expression metacharacters, regular grep does not recognize the
backslash, the character was translated into extended regular expressions, like egrp
and
grep -E like

2. sed line editor

stream editor
processing pure ASCII code for operating the text processing, the line currently being processed is stored in a temporary buffer, called a "model space" (pattern space) to specify which row sed process only mode condition does not meet the meet the conditions Pass the
process after completion of the contents of the buffer sent to the screen
followed by processing the next line, this is repeated until the end of file command format Sed

Calls sed command has two forms:

sed [options] 'command' file(s) 
sed [options] -f scriptfile file(s)

sed on the character of the process •

p 显示 • 
d 删除 • 
a 添加 • 
c 替换 • 
w 写入 • 
i 插入

• p mode operation

But -n '/: / p fstab •
sed -n' / UUID $ / p fstab •
sed -n '/ ^ UUID / p fstab •
But -n' 2,6p 'fstab •
But -n' 2 , 6 * p 'fstab

• d mode operation

sed '/^UUID/d' /etc/fstab • 
sed '/^#/d' /etc/fstab • 
sed '/^$/d'/etc/fstab • 
sed '1,4d'/etc/fstab • 
sed –n '/^UUID/!d' /etc/fstab

a mode of operation

But '/ ^ UUID / a \ war, but the / etc / fstab
and / ^ UUID / a \ war, but \ nwestos / etc / fstab'i模式操作
but' / ^ UUID / i \ war, but \ nwestos / etc / fstab c模式操作
but '/ ^ UUID / c \ war, but \ nwestos / etc / fstab'

• w mode operation

sed '/^UUID/w /tmp/fstab.txt' /etc/fstab 
sed -n'/^UUID/w /tmp/fstab.txt' /etc/fstab 
sed '/^UUID/='/etc/fstab 
sed '6r /etc/issue' /etc/fstab

Other uses of sed

sed -n '/^UUID/=' fstab
sed -n -e '/^UUID/p' -e '/^UUID/=' fstab 
sed -e 's/brown/green/; s/dog/cat/' data 
sed -f rulesfile file 
sed 's/^\//#/'/etc/fstab  
sed 's@^/@#@g'/etc/fstab  
sed 's/\//#/'/etc/fstab  
sed 's/\//#/g/'/etc/fstab
sed 'G' data 
sed '$!G' data 
sed '=' data | sed 'N; s/\n/ /' 

Guess you like

Origin blog.csdn.net/zhaoliang_Guo/article/details/91076120