Use of awk/sed to filter out fdisk output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use of awk/sed to filter out fdisk output
# 1  
Old 04-28-2014
Use of awk/sed to filter out fdisk output

Hi ,
I am trying to filter out the below output of fdisk -l command :
Code:
 
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 :
Code:
 
Disk /dev/sda:63 sectors/track

format 2:
Code:
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
# 2  
Old 04-28-2014
Hello Omkar,

Could you please give us the expected output please.


Thanks,
R. Singh
# 3  
Old 04-28-2014
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.
# 4  
Old 04-28-2014
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
Code:
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

Code:
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

Last edited by temp_user; 04-28-2014 at 08:54 AM..
# 5  
Old 04-28-2014
Hello Omkar,

Following may help.

Code:
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.

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


Thanks,
R. Singh
# 6  
Old 04-28-2014
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 :
Code:
 
Disk /dev/sda 255 heads

command 2 output :
Code:
 
Disk /dev/sda  63 sectors/track

thank you again Smilie
# 7  
Old 04-28-2014
Hello Omkar,

Here are the 2 seprate commands as requested.

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

2. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

3. Shell Programming and Scripting

Complex Filter using grep, awk or sed

Hi, I'm not very familiar witrh sed or awk and hope the somebody can help me to solve my problem. I need to filter a text report using grep, sed or awk. I would like to cut out text lines with the pattern INFO and if exists the following lines of the pattern DETAILS. I need te keep the lines with... (4 Replies)
Discussion started by: Frankg
4 Replies

4. Red Hat

Make a disk disappear from fdisk output

Hello, 1 ) Fdisk -l # Displays all the disk with partition table information My Query ) A ) How can i make one disk ex: /dev/sdd not visible in fdisk -l output ? B) From where fdisk -l collect and display the information ? (8 Replies)
Discussion started by: saurabh84g
8 Replies

5. Shell Programming and Scripting

Use of awk or sed to filter out the ouput

Hi, i am trying to get the system model with the help of awk : $ prtconf | awk '/^System Model/' System Model: IBM,8408-E8D but i want just the below outout that is command should chk for pattern <IBM,> and remove it from the final output : System Model:8408-E8D Can... (2 Replies)
Discussion started by: omkar.jadhav
2 Replies

6. Shell Programming and Scripting

Use of awk to filter out the command output

Hi All, I am trying to find out number of cores present for hp-ux server from the output of print_manifest (as shown below). i suppose awk will be best tool to use for filtering. output of print_manifest is : System Hardware Model: ia64 hp Integrity Virtual Partition ... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

7. UNIX for Dummies Questions & Answers

How can we identify SAN devices in fdisk -l output.?

We can Know the storage devices in fdisk -l. But Please tell me how can i identify the SAN devices :wall: How SAN devices are represented in the fdisk -l output :wall::wall: Thanks in Advance........:D (3 Replies)
Discussion started by: vamshigvk475
3 Replies

8. Shell Programming and Scripting

Need to build a grep/sed/awk filter

Hi I need to to direct only the path and the name of the trace file to a new file. How do I use grep/awk/sed filter? eg. ABC.root>cat alert_omc_dg.log | grep trc ORA-00060: Deadlock detected. More info in file /u01/oradata/omc/udump/omc_dg_ora_3555.trc. ORA-00060: Deadlock detected. More... (8 Replies)
Discussion started by: geetap
8 Replies

9. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

filter parts of a big file using awk or sed script

I need an assistance in file generation using awk, sed or anything... I have a big file that i need to filter desired parts only. The objective is to select (and print) the report # having the string "apple" on 2 consecutive lines in every report. Please note that the "apple" line has a HEX... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question