LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2011, 07:09 AM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Need help in displaying particular lines via grep


So For example I run this command

df -h

I get

Code:
Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   373Gi  271Gi  102Gi    73%    /
devfs          178Ki  178Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
But want to gerp / cut it in such a way that it only displays

Code:
/dev/disk0s2   373Gi  271Gi  102Gi    73%    /
devfs          178Ki  178Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
Now the thing is that these 3 lines are not static.. there can be N number of lines there.. the only thing is that I want the command / output NOT to display the first line but the rest of the n lines .. how can I do this?

thanks
 
Old 04-08-2011, 07:15 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
What have you tried - what didn't work ?.
 
Old 04-08-2011, 07:18 AM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Well

I tried a tail but then that will get stuck with limited lines

So if I do a df -h | tail -3

I will get that 3 lines.. But if I have more mounts.. they will not show.. and only the last three lines will..
 
Old 04-08-2011, 07:31 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
If you always want lines 2 till the end, try sed - it has an option for lines ranges that includes a non-specific descriptor for the last line.
awk or perl can also handle it easily.
 
Old 04-08-2011, 07:35 AM   #5
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
I am quite a n00b.. Can u show how to do it? I am not sure..
 
Old 04-08-2011, 07:46 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Start here.
The sed one-liners is a (very) handy reference.
 
Old 04-08-2011, 07:53 AM   #7
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
Start here.
The sed one-liners is a (very) handy reference.
Awesome

so this works

Code:
df -h | sed '/Size/d'
Any better / more elegent way to do it? or is this good?
 
Old 04-08-2011, 08:00 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Many ways to skin a cat.
Whatever works - I'd suggest you also try variants using line (number) ranges.
 
Old 04-08-2011, 08:42 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
To remove the first line:

Code:
df -h | sed '1d'
 
Old 04-08-2011, 09:03 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using tail you can specify the line to start printing till the end of the file:
Code:
df -h | tail -n +2
Just to add an awk solution as per "many ways to skin a cat" cited above:
Code:
df -h | awk 'NR > 1'
An aside note: if you want to process the output of the df command in a script, better to use the -P (portability) option. It ensures that the output is not splitted into multiple lines, for example when the device name is long. For example on a machine having logical volumes with default names:
Code:
$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/isw_dcjhjdcjac_Volume0p2
                      379G   11G  348G   4% /
/dev/mapper/VolGroup00-LogVol00
                      1.8T  212G  1.5T  13% /home
/dev/mapper/isw_dcjhjdcjac_Volume0p1
                      114M   24M   85M  22% /boot
tmpfs                 5.9G     0  5.9G   0% /dev/shm
/dev/sdi1             917G  170G  701G  20% /media/backup_op_004
$ df -Ph
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/isw_dcjhjdcjac_Volume0p2  379G   11G  348G   4% /
/dev/mapper/VolGroup00-LogVol00  1.8T  212G  1.5T  13% /home
/dev/mapper/isw_dcjhjdcjac_Volume0p1  114M   24M   85M  22% /boot
tmpfs                 5.9G     0  5.9G   0% /dev/shm
/dev/sdi1             917G  170G  701G  20% /media/backup_op_004
The second one is less human readable, but more manageable from a script. Just my
 
  


Reply



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
grep lines before and after magnum81 Programming 3 07-29-2011 04:52 PM
GREP: how to specify a line or lines where to search carolflb Linux - Newbie 7 11-23-2009 08:45 AM
Getting only unique lines using grep.. sph90457 Linux - Newbie 6 07-05-2007 08:31 AM
How to grep similar lines in bash? bruno buys Linux - Software 2 12-02-2005 11:56 PM
Grep lines gsibble Linux - General 1 04-12-2004 01:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

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