Use of awk/sed to filter out fdisk output

Hi ,
I am trying to filter out the below output of fdisk -l command :

 
fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        5222    41838617+  83  Linux

in to below format:
format 1 :

 
Disk /dev/sda:63 sectors/track

format 2:

Disk /dev/sda:255 heads

Can someone please provide me some hint or combination of awk/sed to get format 1 and format 2 output.

thanks

Hello Omkar,

Could you please give us the expected output please.

Thanks,
R. Singh

Hi Ravinder,

Please find the below 2 forms in which outputs are required

Disk /dev/sda:255 heads

and

Disk /dev/sda:63 sectors/track

That is one command which can give count of heads and another for providing sectors/track.

Hi Omkar,

U can try the below thing. Its a very quick thing which i have done. The output u will get with these commands is also below

a=`fdisk -l | awk 'NR==2 {print $1 " " $2} NR==3 {print $3 " " $4}'`
b=`echo $a | tr -d ','`

Output: Disk /dev/sda:63 sectors/track

a=`fdisk -l | awk 'NR==2 {print $1 " " $2} NR==3 {print $1 " " $2}'`
b=`echo $a | tr -d ','`

Output: Disk /dev/sda: 255 heads

Hello Omkar,

Following may help.

fdisk -l | awk -F":|," '/Disk/ {a=$1}; /heads/ {b=$1;c=$2} END{print a OFS b ORS a OFS c}'

Ouptut will be as follows.

Disk /dev/sda 255 heads
Disk /dev/sda  63 sectors/track

Thanks,
R. Singh

thank you very much Ravinder and temp user for the commands .

Ravinder - Can you please show me how the command can be split into two so that it can give two separate output as below :
command 1 output :

 
Disk /dev/sda 255 heads

command 2 output :

 
Disk /dev/sda  63 sectors/track

thank you again :slight_smile:

Hello Omkar,

Here are the 2 seprate commands as requested.

fdisk -l | awk -F":|," '/Disk/ {a=$1}; /heads/ {b=$1;} END{print a OFS b}'
fdisk -l | awk -F":|," '/Disk/ {a=$1}; /heads/ {c=$2;} END{print a OFS c}' 

Thanks,
R. Singh

Thanks a ton Ravinder .. :slight_smile:

---------- Post updated at 10:43 AM ---------- Previous update was at 09:52 AM ----------

Hi Ravinder,

The combination of awk and fdisk is working absolutely fine on RedHat machines.
I have tried same commands on SUSE linus for which i got below output :

fdisk -l | awk -F":|," '/Disk/ {a=$1}; /heads/ {b=$1;} END{print a OFS b}'
Disk identifier 255 heads
 
fdisk -l | awk -F":|," '/Disk/ {a=$1}; /heads/ {c=$2;} END{print a OFS c}'
Disk identifier  63 sectors/track

as you can see this is because there are two lines or patterns which starts with 'Disk' :

 
fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000367ba
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         191     1534176   82  Linux swap / Solaris
/dev/sda2   *         192        5152    39849232+  83  Linux

Could you asist how we can take Disk /dev/sda part rather than Disk identifier.

Hello Omkar,

Could you please try the following.

fdisk -l |  awk -F":|,"  '/Disk \/.*/ {a=$1}; /heads/ {c=$2;} END{print a OFS c}' 

Thanks.
R. Singh

Excellent :). Please find the below output from SUSE linux and RedHat Linux machines :

 
VM172016001141:~ # fdisk -l |  awk -F":|,"  '/Disk \/.*/ {a=$1}; /heads/ {c=$2;} END{print a OFS c}'
Disk /dev/sda  63 sectors/track
VM172016001141:~ # fdisk -l |  awk -F":|,"  '/Disk \/.*/ {a=$1}; /heads/ {b=$1;} END{print a OFS b}'
Disk /dev/sda 255 heads

Thnaks a ton Rakesh for your guidance and help...It will be great if you can provide me some link/pdf so that i can increase my knowledge on awk or sed .
Thanks a gain :slight_smile: