【一练】sed/awk单行脚本练习(选择性删除特定行)

上一篇 / 下一篇  2018-01-17 11:10:58 / 个人分类:shell

5、选择性删除特定行。
(1)显示通篇文档,除了两个正则表达式之间的内容。
>> cat num2.txt 
>> 1234
   regex123
   234
   345regex
   3456
   1234
   12345
   123456
   1234565
   1234543
   12
>> sed '/regex123/,/345rege/d' num2.txt 或 awk '/regex123/{F=1}{if(!F)print}/345regex/{F=0}' num2.txt 
>> 1234
   3456
   1234
   12345
   123456
   1234565
   1234543
   12
(2)删除文件中相邻的重复行。
>> cat num9 
   this is a test
   this is a test
   1234567890 12345
   this is a test
   12345
   1234567890 12345
   qazwsxedcrfv
   rfvrgtby
>> sed '$!N;/^\(.*\)\n\1$/!P;D' num9 或 awk        '{if($0!=B)print;B=$0}' num9 
   this is a test
   1234567890 12345
   this is a test
   12345
   1234567890 12345
   qazwsxedcrfv
   rfvrgtby
(3)删除除重复行外的所有行。
>> cat num10
   Jane the USA 21
   Jane the USA 21
   Mary Cuba 22
   Kangkang China 21
   Mike England 20
>> sed '$!N;s/^\(.*\)\n\1$/\1/;t;D' num10 或 awk '{if($0==B && $0!=l){print;l=$0}B=$0}' num10 
   Jane the USA 21
(4)删除文件中开头的10行。
>> cat num9 
   1this is a test
   2this is a test
   31234567890 12345
   4this is a test
   512345
   61234567890 12345
   7qazwsxedcrfv
   8rfvrgtby
   9this is a test
   10this is a test
   111234567890 12345
   12this is a test
   1312345
   141234567890 12345
   15qazwsxedcrfv
   16rfvrgtby
>> sed '1,10d' num9 或 awk '{if(NR>10)print}' num9
   111234567890 12345
   12this is a test
   1312345
   141234567890 12345
   15qazwsxedcrfv
   16rfvrgtby
(5)删除文件中的最后一行。
>> sed '$d' num9 或 awk '{B[NR]=$0}END{for(i=0;i<=NR-1;i++)print B[i]}' num9
>> 1this is a test
   2this is a test
   31234567890 12345
   4this is a test
   512345
   61234567890 12345
   7qazwsxedcrfv
   8rfvrgtby
   9this is a test
   10this is a test
   111234567890 12345
   12this is a test
   1312345
   141234567890 12345
   15qazwsxedcrfv
(6)删除文件中的最后两行。
>>  sed 'N;$!P;$!D;$d' num9 或 awk '{B[NR]=$0}END{for(i=0;i<=NR-2;i++)print B[i]}' num9
>> 1this is a test
   2this is a test
   31234567890 12345
   4this is a test
   512345
   61234567890 12345
   7qazwsxedcrfv
   8rfvrgtby
   9this is a test
   10this is a test
   111234567890 12345
   12this is a test
   1312345
   141234567890 12345
(7)删除文件中的最后10行。
>> sed -n -e :a -e '1,10!{P;N;D;};N;ba' num9 或  awk '{B[NR]=$0}END{for(i=0;i<=NR-10;i++)print B[i]}' num9

>> 1this is a test
   2this is a test
   31234567890 12345
   4this is a test
   512345
   61234567890 12345
(8)删除8的倍数行。
>> sed 'n;n;n;n;n;n;n;d;' num9 或 awk '{if(NR%8!=0)print}' num9 
   1this is a test
   2this is a test
   31234567890 12345
   4this is a test
   512345
   61234567890 12345
   7qazwsxedcrfv
   9this is a test
   10this is a test
   111234567890 12345
   12this is a test
   1312345
   141234567890 12345
   15qazwsxedcrfv
(9)删除匹配样式的行。
>> sed '/test/d' num9 或 awk '{if(!match($0,/test/))print}' num9 
   31234567890 12345
   512345
   61234567890 12345
   7qazwsxedcrfv
   8rfvrgtby
   111234567890 12345
   1312345
   141234567890 12345
   15qazwsxedcrfv
   16rfvrgtby
(10)删除文件中的所有空行。
>> cat num1.txt 
   11 1



   11 111

   12 123

   1 23

>> sed '/^$/d' num1.txt 或 awk '{if(!match($0,/^$/))print}' num1.txt 
>> 11 1
   11 111
   12 123
   1 23



TAG:

 

评分:0

我来说两句

Open Toolbar