sed 删除最后几行 和删除指定行
转载原文链接:
sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的。
删除指定的行【可以指定行号删除、匹配字符串来删除】
[root@Jason64-17 ~]# cat -n seq.txt 1 ok i will help you 2 understand sed usage 3 how to use it 4 and we should use it in view 5 now let us 6 go 7 hello my name is[root@Jason64-17 ~]# cat -n seq.txt | sed 3d 1 ok i will help you 2 understand sed usage 4 and we should use it in view 5 now let us 6 go 7 hello my name is[root@Jason64-17 ~]# cat -n seq.txt | sed /should/d 1 ok i will help you 2 understand sed usage 3 how to use it 5 now let us 6 go 7 hello my name is[root@Jason64-17 ~]# cat -n seq.txt | sed /ow/d 1 ok i will help you 2 understand sed usage 4 and we should use it in view 6 go 7 hello my name is[root@Jason64-17 ~]# cat -n seq.txt | sed -r /how\|should/d 1 ok i will help you 2 understand sed usage 5 now let us 6 go 7 hello my name is
删除最后几行
[root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt12345[root@Jason64-17 ~]# for((i=1;i<4;i++)); do sed -i '$d' seq01.txt ; done #C式for循环 [root@Jason64-17 ~]# cat seq01.txt12[root@Jason64-17 ~]# seq 5 > seq01.txt[root@Jason64-17 ~]# cat seq01.txt 12345[root@Jason64-17 ~]# i=1; while [ $i -le 3 ]; do sed -i '$d' seq01.txt; ((i++)); done#while循环 [root@Jason64-17 ~]# cat seq01.txt12[root@Jason64-17 ~]# seq 5 > seq01.txt[root@Jason64-17 ~]# cat seq01.txt12345[root@Jason64-17 ~]# for i in `seq 3`; do sed -i '$d' seq01.txt ; done#bash for循环[root@Jason64-17 ~]# cat seq01.txt 12[root@Jason64-17 ~]# seq 5 > seq01.txt[root@Jason64-17 ~]# cat seq01.txt 12345#until 循环[root@Jason64-17 ~]# seq 5 > seq01.txt[root@Jason64-17 ~]# cat seq01.txt 12345[root@Jason64-17 ~]# i=3; until [ $i -le 0 ]; do sed -i '$d' seq01.txt ; ((i--)); done[root@Jason64-17 ~]# cat seq01.txt12[root@Jason64-17 ~]#
awk练习题
wang 4 cui 3 zhao 4 liu 3 liu 3 chang 5 li 2
1 通过第一个域找出字符长度为4的 2 当第二列值大于3时,创建空白文件,文件名为当前行第一个域$1 (touch $1) 3 将文档中 liu 字符串替换为 hong 4 求第二列的和 5 求第二列的平均值 6 求第二列中的最大值 7 将第一列过滤重复后,列出每一项,每一项的出现次数,每一项的大小总和
1、字符串长度 awk 'length($1)=="4"{print $1}' 2、执行系统命令 awk '{if($2>3){system ("touch "$1)}}' 3、gsub(/r/,"s",域) 在指定域(默认$0)中用s替代r (sed 's///g') awk '{gsub(/liu/,"hong",$1);print $0}' a.txt 4、列求和 df -h | awk '{a+=$2}END{print a}' 5、列求平均值 df -h | awk '{a+=$2}END{print a/NR}' df -h | awk '{a+=$2;b++}END{print a,a/b}' 6、列求最大值 df -h | awk 'BEGIN{a=0}{if($2>a) a=$2 }END{print a}' 7、将第一列过滤重复列出每一项,每一项的出现次数,每一项的大小总和 awk '{a[$1]++;b[$1]+=$2}END{for(i in a){print i,a[i],b[i]}}'