【问题标题】:Print specific lines from file打印文件中的特定行
【发布时间】:2019-11-30 14:31:17
【问题描述】:

我有一个这样的文本文件:

OBJ O1 = {
{P11},
{0}
};

OBJ O2 = {
{P21},
{P22},
{P23},
{0}
};

OBJ O3 = {
{P31},
{P32},
{0}
};

我只想打印以OBJ O2 开头并在OBJ O2 之后以第一个{0} 结尾的行

OBJ O2 = {
{P21},
{P22},
{P23},
{0}
};

是否有使用 cat、sed、awk 或 grep 打印这些行的解决方案?

【问题讨论】:

    标签: search awk sed grep cat


    【解决方案1】:

    你可以这样做:

    awk '/OBJ O2/,/^};$/' file
    

    【讨论】:

    • awk '$2=="O2"' RS= inputawk '$1$2=="OBJO2"' RS= input
    • awk '/^OBJ O2/' RS= input
    • 知道了The empty string "" (a string without any characters) has a special meaning as the value of RS. It means that records are separated by one or more blank lines and nothing else. - 在这种情况下效果很好!
    【解决方案2】:

    一个 awk 替代方案(没有范围表达式),参见。 Is a /start/,/end/ range expression ever useful in awk?

    awk '/^OBJ O2/{f=1} f{print; if(/};/) f=0}' file
    

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多