LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-25-2009, 11:22 PM   #1
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Rep: Reputation: 38
Need a little help with grep syntax


Hi

I am trying to print only a specific device using fdisk -l piped through grep. Specifically, I am looking to print only "/dev/sda1" However the entire line is printed including format and partition size. I want to cut off the rest of the line and only print the beginning of the line.

For example,
fdisk -l prints
Code:
Disk /dev/hdc: 60.0 GB, 60011642880 bytes
255 heads, 63 sectors/track, 7296 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x02670266

   Device Boot      Start         End      Blocks   Id  System
/dev/hdc1   *           1        5840    46909768+  83  Linux
/dev/hdc2            5841        5948      867510   82  Linux swap
/dev/hdc3            5949        7296    10827810   83  Linux

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00073443

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1       11917    95723271    c  W95 FAT32 (LBA)
/dev/sda2           11918       37413   204796620   83  Linux
/dev/sda3           37414      108636   572098747+   f  W95 Ext'd (LBA)
/dev/sda5           37414       62909   204796588+   c  W95 FAT32 (LBA)
/dev/sda6           62910       75804   103579056    b  W95 FAT32

fdisk -l | grep Linux| grep -w '/dev/sd..' prints the entire line
Quote:
/dev/sda2 11918 37413 204796620 83 Linux
How would I cut off the rest of the line and print only /dev/sda2 ?

Last edited by okos; 01-25-2009 at 11:33 PM.
 
Old 01-25-2009, 11:38 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
fdisk -l | grep Linux| grep -o '/dev/sd..'

or

fdisk -l | grep Linux |cut -d " " -f 1
There may be even better ways to go about this. You could probably do the whole job with a single awk or perl command, for example.
 
Old 01-25-2009, 11:50 PM   #3
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by David the H. View Post
Code:
fdisk -l | grep Linux| grep -o '/dev/sd..'

or

fdisk -l | grep Linux |cut -d " " -f 1
There may be even better ways to go about this. You could probably do the whole job with a single awk or perl command, for example.




The first one worked ! What does -o mean?

The second did not work. It printed
Code:
/dev/hdc1
/dev/hdc2
/dev/hdc3
/dev/sda2

EDIT
Never Mind about the question.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such
part on a separate output line.

Last edited by okos; 01-25-2009 at 11:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command syntax question for grep kaplan71 Linux - Software 4 07-16-2008 02:13 PM
Need help with grep syntax and reg exp Rush_898 Linux - General 2 01-08-2008 10:28 AM
grep CLI syntax: how to find A OR B? Kropotkin Linux - Newbie 3 01-16-2007 09:45 PM
grep script/syntax carriehoff Linux - Newbie 6 08-31-2006 01:13 PM
Grep syntax questions Phaethar Linux - General 4 04-11-2005 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration