Multiline edit in awk

[亡魂溺海] 提交于 2019-12-11 06:57:14

问题


MV: The Garden Murder Case (1936)
-------------------------------------------------------------------------------
MV: The Garden of Allah (1936)
BT: USD 2,200,000 
-------------------------------------------------------------------------------

In the above format my record separator is the line of ------

I want to drop all single line records so above the first record should drop and the 2nd should be kept.

Simple enough so I thought.

BEGIN {RS="^[-]+$"}
  $0 !~ /^(BT|GR|OW|RT|AD)/ {next}
  1

I also tried checking for a record that contains 2 end of line characters

BEGIN {RS="^[-]+$"}
  /$.+$/
  1 {next}

Neither worked.


回答1:


You can't use the ^ and $ in the Record separator as they are the start and end of records based on the record separator.

Try this

awk -vRS="\n-+\n" -F"\n" 'NF>1' file

Output

MV: The Garden of Allah (1936)
BT: USD 2,200,000 

If you want to retain the field separators then you can use

awk -vRS="\n-+\n" -F"\n" 'NF>1{printf "%s%s",$0,RT}' file

Input

MV: The Garden Murder Case (1936)
-------------------------------------------------------------------------------
MV: The Garden of Allah (1936)
BT: USD 2,200,000
-------------------------------------------------------------------------------
MV: The Garden Murder Case (1936)
-------------------------------------------------------------------------------
MV: The Garden of Allah (1936)
BT: USD 2,200,000
-------------------------------------------------------------------------------

output

MV: The Garden of Allah (1936)
BT: USD 2,200,000
-------------------------------------------------------------------------------
MV: The Garden of Allah (1936)
BT: USD 2,200,000
-------------------------------------------------------------------------------


来源:https://stackoverflow.com/questions/27860634/multiline-edit-in-awk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!