基础命令
echo
[root@k8s-master python3-test]# echo -n "" >test.txt
[root@k8s-master python3-test]# cat test.txt
[root@k8s-master python3-test]#
#这样清空文件,不会有空行,否则会有如下:
[root@k8s-master python3-test]# echo > test.txt
[root@k8s-master python3-test]# cat test.txt
[root@k8s-master python3-test]#
##echo -n 不换行输出
Sed
所有增删改在文件中要想真实改变,要加-i参数:sed -i '2,3d'
[root@k8s-master bash]# sed -n 'x;$p' ping.sh ##打印文本中倒数第二行内容
[root@k8s-master bash]# sed -n '3,6p' /etc/passwd ##打印第3-6行内容
[root@k8s-master bash]# sed -n '1p;4p' /etc/passwd ##打印第一行和第四行
[root@k8s-master bash]# sed -n '/root/p' /etc/passwd ##打印匹配到包含root行
[root@k8s-master bash]# sed -n '/root$/p' /etc/passwd ##打印root结尾的行
[root@k8s-master bash]# sed -n 'p' 2.sh ##打印文件所有内容
[root@k8s-master bash]# sed 'd' 2.sh ##删除所有内容
[root@k8s-master bash]# sed -n '/^bin/p' /etc/passwd ##打印bin开头的行
[root@k8s-master bash]# sed -n '$=' 2.sh ##打印该文件行数,一共多少行
[root@k8s-master bash]# sed '/echo/!d' 2.sh ##删除不包含echo的行
[root@k8s-master bash]# sed '/^echo/d' 2.sh ##删除echo开头的行
[root@k8s-master bash]# sed '$d' 2.sh #删除文件最后一行
[root@k8s-master bash]# sed '/^$/d' 2.sh #删除文件所有空行
[root@k8s-master bash]# sed 's/root/ROOT/g' 2.sh ##将所有root都换成ROOT
[root@k8s-master bash]# sed 's/root//g' 2.sh ##将所有root字符串删除(替换为空格)
[root@k8s-master bash]# sed '4,7s/^/#/' 2.sh ##4-7行添加注释
[root@k8s-master bash]# sed '4,7s/^#//' 2.sh ##4-7行解开注释
[root@k8s-master bash]# sed '/echo/c 33echo22' 2.sh ##将echo所在的行,整行替换为33echo22
[root@k8s-master bash]# sed '3c AAAA' 2.sh ##将第三行整行替换为AAAA
[root@k8s-master bash]# sed '$r /etc/passwd' 2.sh ##将/etc/passwd中的所有内容追加到2.sh中
[root@k8s-master bash]# sed '3w 2.sh' /etc/passwd ##将/etc/passwd中的第3行内容写到2.sh(覆盖)
[root@k8s-master bash]# sed '1,3w 2.sh' /etc/passwd ##将/etc/passwd中的第1-3行内容写到2.sh(覆盖)
[root@k8s-master bash]# sed '/^#/!w 2.sh' /etc/inittab ##将/etc/inittab中的非#开头的内容写到sh中