正则表达式
-v:取反
-n:显示行号
-ni:不区分大小写
查找 删除 替换
根据字符,字符串,进行匹配
文本编辑三剑客
grep:查询搜索过滤
sed:查询搜索过滤修改
awk
shirt
short
任意单个字符
*0个或多个字节
+1个或者
vim test.txt
he was short and fat.
he was weating a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
PI=3.14
a wood cross!
Actions speak louder than words
#woood #
#woooooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
cat test.txt
grep root /etc/passwd
grep -v root /etc/passwd
grep the test.txt
grep -v the test.txt
grep -n the test.txt
grep -ni the test.txt
grep sh[io]rt test.txt
grep -n sh[io]rt test.txt
grep -n [^w]oo test.txt 上箭头写到中括号里面取反,
grep -n [^a-z]oo test.txt 筛选不是小写字母开头的
grep -n ^[a-z]oo test.txt 以小写字母的开头
grep -n ^[^a-z]oo test.txt 以非小写字母开头的
grep -n the test.txt 这一行任何位置出现的
grep -n ^the test.txt 这一行开头显示的
grep -n the$ test.txt 以the结尾的
grep -n ^[a-zA-Z] test.txt
grep -n !$ test.txt
grep -n '!$' test.txt
grep -n '.$' test.txt
grep -n "wood" test.txtgrep -n "w..d" test.txt
grep -n "w..." test.txt
grep -n 'o*' test.txt
grep -n 'oo*' test.txt *零次和多次
grep -n 'w.*d' test.txt 任意的多个参数
grep -n 'w.*d' test.txt 以W开头,d为结尾
grep - '[0-9][0-9]*' test.txt 匹配数字
grep -n 'o\{2\}' test.txt
grep -n 'wo\{2,5\}d' test.txt wo开头d结尾中间有2-5个
grep -n 'wo\{2,\}d' test.txt 无限制
grep -n -v '^$' /etc/ssh/sshd_config 去除空行
grep -v '^#' /etc/ssh/sshd_config 去除注释
egrep -v '^$|^#' /etc/ssh/sshd_config
-------------------------------------------------------------------------------------------------
sed
sed -n test.txt
sed -n 'p' test.txt 打印指定的文本
sed -n '10p' test.txt 输出第10行
sed -n '10,13p' test.txt 输出第10-13行
sed -n 'p;n' test.txt 输出奇数行
sed -n 'n;p' test.txt 输出偶数行
sed -n '1,10{n;p}' test.txt 1-10的偶数行
sed -n '1,${n;p}' test.txt 到最后
sed -n '/the/p' test.txt 第一个过滤某个单词,第二个操作符
sed -n '/the/=' test.txt 查看行号
sed -n '/^PI/p' test.txt 过滤PI开头的
sed -n '/wood/p' test.txt sed -n '/\<wood\>/p' test.txt 查找单词
sed '10d' test.txt 删除第十行
nl test.txt | sed '10d' 显示行号删除
nl test.txt | sed '10,13d' 删除10-13行
nl test.txt | sed '/the/d' 删除单词the
nl test.txt | sed '/the/!d' 删除除了单词the的
sed '/^[a-z]/d' test.txt 删除小写字母开头的
sed '/^$/d' test.txt 删除空行
sed '/\.$/p' test.txt
sed 's/the/THE/' test.txt 替换小写the
vim test.txt
he was short and fat.
he was weating a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is the boneless but the it breaks bones.12!
google is the best tools for search keyword.
PI=3.14
a wood cross!
Actions speak louder than words
#woood #
#woooooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
sed 's/the/THE/g' test.txt 全区替换+g
sed 's/the//g' test.txt 删除THE
sed 's/^/#/' test.txt 每一行行首加#
sed 's/$/#/' test.txt 每一行行尾加#
sed '/the/s/^/#/' test.txt 包含the的行首加#
sed '/the/s/^/fsas/' test.txt 在包含the的行首添加单词
sed '/the/s/$/fsas/' test.txt 在包含the的行尾添加单词
sed '3,5s/the/THE/g' test.txt 指定替换3-5的the
sed '/the/s/o/A/g' test.txt 包含the的语句,把里面的小写o替换
sed '/the/{H;d};$G' test.txt 将包含the的行迁移至文件的末尾
sed '1,5{H;d};10G' test.txt 将1-5放在第10行后面
sed '1,5{H;d};20G' test.txt 放到第20行就消失,超限
sed '1a#chkconfig:35 80 20' test.txt 添加到第二行
sed '1i#chkconfig:35 80 20' test.txt 添加到第一行,添加到上一行
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux 修改文件内容的
setenforce 0
------------------------------------------------------------------------------------------------
awk
df | grep /$ | awk '{print $5}'
df | grep /$ | awk '{print $5}' | awk -F% '{print $1}' 去除磁盘利用率的数字
diskusage=$(df | grep /$ | awk '{print $5}' | awk -F% '{print $1}') 存放到diskusage
echo $diskusage
awk -F: '{print}' /etc/passwd awk输出,间隔符可以省略
awk -F: '{print $1}' /etc/passwd 按列输出
awk -F: '{print $1,$2}' /etc/passwd 支持多行输出
awk -F: '{print $0}' /etc/passwd 全部列
awk -F: 'NR==3,NR==6{print}' /etc/passwd 显示第3行到第6行
awk -F: '(NR%2)==1{print}' /etc/passwd 奇数行
awk -F: '(NR%2)==0{print}' /etc/passwd 偶数行
awk -F: '/^root/{print}' /etc/passwd 以root开头的行
awk -F: '/nologin$/{print}' /etc/passwd 以nologin结尾的行
------------------------------------------------------------------------------------------------
sort
sort /etc/passwd 给文件a-z排序
sort -r /etc/passwd 反向排序
------------------------------------------------------------------------------------------------
uniq
vim aaa
uniq aaa 删除重复的行
uniq -c aaa 重复的次数
uniq -d aaa 不显示重复的行
------------------------------------------------------------------------------------------------
tr
echo "ACCP" | tr 'A-Z' 'a-z 转换ACCP为小写
echo 'aaccccccppp' | tr -s 'a' 去除a的重复
echo 'aaccccccppp' | tr -s 'ac' 去除ac的重复
echo 'aaccccccppp' | tr -s 'acp' 去除acp的重复
echo 'aaccccccppp' | tr -d 'acp' 删除acp