如何从第二次出现的模式中删除并仅删除文件中的所有剩余事件?
我做了什么:
awk '/Type ;Rho ;DelSqRho ;Bond Ellipticity ;V ;G ;K ;L ;/{p++}p==1' file
Pattern: "Type ;Rho ;DelSqRho ;Bond Ellipticity ;V ;G ;K ;L ;"
输入
Type ;Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C2; 1.1958513197E+02; 1.1958513183E+02; -5.4465523878E+05; NA; -1.3618005730E+05; 8.1237999095E+00; 1.3617193350E+05;
Type ;Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C3; 1.1958827553E+02; 1.1958827351E+02; -5.4468085564E+05; NA; -1.3618586067E+05; 7.8233806895E+00; 1.3617803729E+05;
Type ;Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C4; 1.1959808288E+02; 1.1959808234E+02; -5.4473956436E+05; NA; -1.3620048021E+05; 7.7945613266E+00; 1.3619268565E+05;
预期输出
Type ;Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C2; 1.1958513197E+02; 1.1958513183E+02; -5.4465523878E+05; NA; -1.3618005730E+05; 8.1237999095E+00; 1.3617193350E+05;
(3,-3) NACP C3; 1.1958827553E+02; 1.1958827351E+02; -5.4468085564E+05; NA; -1.3618586067E+05; 7.8233806895E+00; 1.3617803729E+05;
(3,-3) NACP C4; 1.1959808288E+02; 1.1959808234E+02; -5.4473956436E+05; NA; -1.3620048021E+05; 7.7945613266E+00; 1.3619268565E+05;
答案 0 :(得分:3)
与sed
相同:
sed '0,/^Type /b; /^Type /d' file
输出:
Type ;1Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C2; 1.1958513197E+02; 1.1958513183E+02; -5.4465523878E+05; NA; -1.3618005730E+05; 8.1237999095E+00; 1.3617193350E+05;
(3,-3) NACP C3; 1.1958827553E+02; 1.1958827351E+02; -5.4468085564E+05; NA; -1.3618586067E+05; 7.8233806895E+00; 1.3617803729E+05;
(3,-3) NACP C4; 1.1959808288E+02; 1.1959808234E+02; -5.4473956436E+05; NA; -1.3620048021E+05; 7.7945613266E+00; 1.3619268565E+05;
<强>解释强>
/^Type /d
- 删除以Type
0,/^Type /b
- 从文件的开头到匹配Type
的第一行,分支到脚本的末尾 - 也就是说,跳过删除命令/^Type /
仅从第2行开始测试,从而保留两个Type ...
行。答案 1 :(得分:2)
$ awk '/^Type/{if(p++)next} 1' file
Type ;Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C2; 1.1958513197E+02; 1.1958513183E+02; -5.4465523878E+05; NA; -1.3618005730E+05; 8.1237999095E+00; 1.3617193350E+05;
(3,-3) NACP C3; 1.1958827553E+02; 1.1958827351E+02; -5.4468085564E+05; NA; -1.3618586067E+05; 7.8233806895E+00; 1.3617803729E+05;
(3,-3) NACP C4; 1.1959808288E+02; 1.1959808234E+02; -5.4473956436E+05; NA; -1.3620048021E+05; 7.7945613266E+00; 1.3619268565E+05;
答案 2 :(得分:0)
您可以使用以下awk命令:
awk '$0 !~ /Type ;Rho.* ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;/ || FNR < 2' infile
它有两个条件,FNR < 2
打印第一行,标题。另一行每一行都与标题不匹配。
它产生:
Type ;Rho ;Rho at Nucleus ;DelSqRho ;Bond Ellipticity ;V ;G ;K; L;
(3,-3) NACP C2; 1.1958513197E+02; 1.1958513183E+02; -5.4465523878E+05; NA; -1.3618005730E+05; 8.1237999095E+00; 1.3617193350E+05;
(3,-3) NACP C3; 1.1958827553E+02; 1.1958827351E+02; -5.4468085564E+05; NA; -1.3618586067E+05; 7.8233806895E+00; 1.3617803729E+05;
(3,-3) NACP C4; 1.1959808288E+02; 1.1959808234E+02; -5.4473956436E+05; NA; -1.3620048021E+05; 7.7945613266E+00; 1.3619268565E+05;