且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除bash中的比赛之前和之后的行(使用sed或awk)?

更新时间:2023-12-03 22:39:40

sed会做到:

sed '/\n/!N;/\n.*\n/!N;/\n.*\n.*PINITIAL BALANCE/{$d;N;N;d};P;D'

它是这样工作的:

  • 如果sed在模式空间中只有一个字符串,则它会连接另一个字符串
  • 如果只有两个,它将加入第三个
  • 如果确实使用BALANCE补齐了LINE + LINE + LINE的模式,它将联接以下两个字符串,将其删除并从头开始
  • 如果不是,它将打印出模式中的第一个字符串并将其删除,并从头开始而不用滑动模式空间

为防止在第一个字符串上出现图案,应修改脚本:

To prevent the appearance of pattern on the first string you should modify the script:

sed '1{/PINITIAL BALANCE/{N;N;d}};/\n/!N;/\n.*\n/!N;/\n.*\n.*PINITIAL BALANCE/{$d;N;N;d};P;D'

但是,如果您要删除的另一个字符串PINITIAL BALANCE失败了.但是,其他解决方案也失败=)

However, it fails in case you have another PINITIAL BALANCE in string which are going to be deleted. However, other solutions fails too =)