通过终端中的开始和结束模式选择文本范围

时间:2017-03-21 06:33:55

标签: regex shell awk sed terminal

鉴于打开和关闭模式,我想选择该范围内的所有文本,包括与此模式匹配的新行。

对于前。

➜  diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                            Macintosh HD           +249.8 GB   disk1
                                 Logical Volume on disk0s2
                                 A200C898-46E7-4C85-B946-35677A6880C6
                                 Unlocked Encrypted

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS m                       999.3 GB   disk2s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk2s3

我有兴趣选择外置硬盘及其所有条目。开场模式(我想到的)将是/(external, physical)/,结束模式应该是/^$|^\n$/

最终结果应为

diskutil list | "magic"
/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS m                       999.3 GB   disk2s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk2s3

3 个答案:

答案 0 :(得分:4)

使用地址范围:

diskutil list | sed '/(external, physical)/,/^$/!d;'

工作原理:

  • /(external, physical)/,/^$/:从匹配(external, physical)的行到下一个空白行
  • !d:不要删除行

答案 1 :(得分:2)

awk版本:awk '/start-string/,/end-string/' input

diskutil list |awk '/external, physical/,/^$/'
/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS m                       999.3 GB   disk2s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk2s3

答案 2 :(得分:1)

如果您要输出的范围不在文件末尾,请使用:

$ awk '/^\/dev\/disk/{f=0} /external, physical/{f=1} f' file
/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS m                       999.3 GB   disk2s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk2s3

说明:

/^\/dev\/disk/ { f=0 }        # flag down at new disk entry
/external, physical/ { f=1 }  # flag up at desired start point
f                             # output when flag up