如何 grep inverted 不显示之前/之后的 x 行?

如何 grep inverted 不显示之前/之后的 x 行?

我需要的是很容易解释但无法做到:-( 我想 grep 命令输出不显示匹配行和 x 行之后那。例如。

# fdisk -l
Disk /dev/ram0: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram1: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

我想跳过带有“ram”字符串的行和之后的 6 行,因此输出将是:

Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

就像是sudo fdisk -l | grep -v "ram" -A 6

答案1

GNU sed 的另一种方法:

| sed '/ram/,/^$/d;/^$/d'

输出:

磁盘 /dev/sda:931.5 GiB,1000204886016 字节,1953525168 个扇区
单位:扇区 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节/4096 字节
I/O 大小(最小/最佳):4096 字节/4096 字节

相关内容