Ihe Onwuka
Ihe Onwuka

Reputation: 477

Multiline edit in awk

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.

Upvotes: 1

Views: 303

Answers (1)

user3442743
user3442743

Reputation:

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
-------------------------------------------------------------------------------

Upvotes: 3

Related Questions