I am trying to get disk information from fdisk -l output on linux.
fdisk -l | grep -E 'Disk /dev/sd.\:'
I get the following output.
Disk /dev/sde doesn't contain a valid partition table
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
but what I want to get is
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
I tried to do this
fdisk -l | grep -E 'Disk /dev/sd.\:' | grep -v "contain"
but I have no idea why grep is not ignoring the line containing “contain”.
Try this command:
OR simply:
Problem is that the line
Disk /dev/sde doesn't contain a valid partition tableis being generated as error and being written onstderrinstead ofstdout.Pipe in unix only pipes output written on
stdoutfrom previous command with the command on RHS of pipe hence your grep command is only working on 2nd and 3rd line while 1st line on your terminal is coming fromstderr.