2

This question already has an answer here:

What I need is easy to explain but couldn't do it :-( I want to grep a command output not showing the matching lines and x lines after that. Eg.

# 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

I want to skip lines with 'ram' string and 6 lines after that, so the output would be just:

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

Something like sudo fdisk -l | grep -v "ram" -A 6

marked as duplicate by don_crissti, techraf, Gilles, GAD3R, HalosGhost Dec 6 '16 at 1:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    The reason it doesn't do what you expect is that -v just inverts the pattern, not the set of lines that ultimately get printed. – DepressedDaniel Dec 5 '16 at 18:27
  • 3
    Since the fdisk output appears to consist of blank-line separated records, a "paragraph mode" approach using awk or perl might be more straightforward e.g. fdisk -l | awk -vRS= '! /ram/' – steeldriver Dec 5 '16 at 18:30
  • 2
    why not just fdisk -l /dev/sda – Valentin Bajrami Dec 5 '16 at 18:32
  • 4
    sed '/ram/,+5 d' – Costas Dec 5 '16 at 18:51
  • grep -A 6 /dev/sd may achieve what you're after. – steve Dec 5 '16 at 20:18
1

Another approach with GNU sed:

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

Output:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.