用bash中的空白行分隔 [英] Separate by blank lines in bash

查看:180
本文介绍了用bash中的空白行分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的输入:

Block 1:
line1
line2
line3
line4

Block 2:
line1
line2

Block 3:
line1
line2
line3

这是一个示例,是否有一种优雅的方式仅在不依赖其名称的情况下打印块2及其行?就像用空白行分隔块并打印第二个块".

This is an example, is there an elegant way to print Block 2 and its lines only without rely on their names? It would be like "separate the blocks by the blank line and print the second block".

推荐答案

将记录分隔符设置为空字符串以在空白行上进行分隔.到 打印第二块:

Set the record separater to the empty string to separate on blank lines. To print the second block:


$ awk -v RS=  'NR==2{ print }'

(请注意,这仅在不包含任何空格的行上分开. 仅包含空格的行不被视为空白行.)

(Note that this only separates on lines that do not contain any whitespace. A line containing only white space is not considered a blank line.)

这篇关于用bash中的空白行分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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