在AWK多行编辑 [英] Multiline edit in awk

查看:136
本文介绍了在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

我也试过检查包含行的字符2月底创下

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

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

但是都没有成功。

Neither worked.

推荐答案

您无法使用 ^ $ 在记录分离器,因为它们是基于所述记录分隔符记录的开始和结束。

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

试试这个

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

输出

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

输入

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

输出

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

这篇关于在AWK多行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆