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

上一篇 / 下一篇  2017-12-28 17:28:45 / 个人分类:shell

4、选择性地显示特定行
(1)显示文件中的前4行。
>> cat num2.txt 
   1234
   regex123
   234
   345regex
   3456
   1234
   12345
   123456
   1234565
   1234543
   12
>> sed 4q num2.txt 或 awk '{print $0;if(NR==4)exit}' num2.txt 
>> 1234
   regex123
   234
   345regex
(2)显示文件第一行。
>> sed q num2.txt 或  awk '{print;exit}' num2.txt 
>> 1234
(3)显示文件的最后10行。
>> sed -e :a -e '$q;N;11,$D;ba' num2.txt  或  awk '{A[NR]=$0}END{for(i=NR-9;i<=NR;i++)print A[i]}' num2.txt 
>> regex123
   234
   345regex
   3456
   1234
   12345
   123456
   1234565
   1234543
   12
(4)显示文件的最后一行。
>> sed '$!d' num2.txt 或 sed -n '$p' num2.txt 或 awk '{A=$0}END{print A}' num2.txt 
>> 12
(5)显示文件的倒数第二行。
>>  sed -e '$!{h;d;}' -e x num2.txt 或 awk '{B=A;A=$0}END{print B}' num2.txt 
>> 1234543
(6)只显示匹配正则表达式的行。
>> sed -n '/regex/p' num2.txt 或 sed '/regex/!d' num2.txt 或 awk '/regex/{print}' num2.txt 
>> regex123
>> 345regex
(7)只显示不匹配正则表达式的行。
>> sed -n '/regex/!p' num2.txt 或 sed '/regex/d' num2.txt 或 awk '!/regex/{print}' num2.txt 
>> 1234
   234
   3456
   1234
   12345
   123456
   1234565
   1234543
   12
(8)查找匹配并将匹配行的上一行显示出来,但不显示匹配行。
>> sed -n '/regex/{g;1!p;};h' num2.txt  或   awk '/regex/{print A}{A=$0}' num2.txt 
>> 1234
   234
(9)查找匹配并将匹配行的下一行显示出来,但不显示匹配行。
>> sed -n '/regex/{n;p;}' num2.txt 或 awk '{if(A)print;A=0}/regex/{A=1}' num2.txt 
>> 234
   3456
(10)显示包含“AAA”、“BBB”、“CCC”的行(任意次序)。
>> cat num8.txt 
   this is a test FOR AAACCCand BBB 123
   aaabbbaaacccbbd
   AAACCC123
   azAAA-BBB-CCC-AAA
>> sed '/AAA/!d;/BBB/!d;/CCC/!d' num8.txt  或 awk '{if(match($0,/AAA/) && match($0,/BBB/) && match($0,/CCC/))print}' num8.txt 
>> this is a test FOR AAACCCand BBB 123
   azAAA-BBB-CCC-AAA
(11)显示包含“AAA”、“BBB”、“CCC”的行(固定次序)。  
>> sed '/AAA.*BBB.*CCC/!d' num8.txt 或 awk '{if(match($0,/AAA.*BBB.*CCC/))print}' num8.txt 
>> azAAA-BBB-CCC-AAA
(12)显示包含“AAA”或“BBB”或“CCC”的行。
>>  sed '/AAA\|BBB\|CCC/!d' num8.txt 或  awk '/AAA|BBB|CCC/{print}' num8.txt 
>> this is a test FOR AAACCCand BBB 123
   AAACCC123
   azAAA-BBB-CCC-AAA
(13)显示包含10个或以上字符的行。
>> sed -n '/^.\{10\}/p' num8.txt 或 awk '{if(length($0)>=10)print}' num8.txt 
>> this is a test FOR AAACCCand BBB 123
>> aaabbbaaacccbbd
>> azAAA-BBB-CCC-AAA
(14)显示包含10个以下字符的行。
>> sed -n '/^.\{10\}/!p' num8.txt 或 sed  '/^.\{10\}/d' num8.txt 或 awk '{if(length($0)<10)print}' num8.txt 
>> AAACCC123
(15)显示部分文本--从包含正则表达式的行开始到最后一行结束。
>> sed -n '/regex/,$p' num2.txt 或 awk '/regex/{F=1}{if(F)print}' num2.txt 
>> regex123
   234
   345regex
   3456
   1234
   12345
   123456
   1234565
   1234543
   12
(16)显示部分文本--指定行号范围(从第3行到第5行)。
>> sed -n '3,5p' num2.txt 或  awk '{if(NR>=3 && (NR<=5))print}' num2.txt 
>> 234
   345regex
   3456
(17)显示第3行。
>> sed -n '3p' num2.txt  或 awk '{if(NR==3)print}' num2.txt 
>> 234
(18)从第3行开始,每2行显示一次。
>> sed -n '3~2p' num2.txt 或 awk '{if(NR==3)F=1}{if(F){i++;if(i%2==1){print}}}' num2.txt 
>> 234
   3456
   12345
   1234565
   12
(19)显示两个正则表达式之间的文本(包含匹配上的部分)。
>> sed -n '/regex123/,/345regex/p' num2.txt 或 awk '/regex123/{F=1}{if(F)print}/345regex/{F=0}' num2.txt
>> regex123
   234
   345regex


TAG:

 

评分:0

我来说两句

Open Toolbar