LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 11-17-2009, 10:52 PM   #1
Thelionroars
LQ Newbie
 
Registered: Aug 2009
Location: Melbourne, Australia
Distribution: Ubuntu
Posts: 19

Rep: Reputation: 1
How to display a file, omitting lines that contain a string?


What would be the simplest command/s to display a file, with lines containing a certain string omitted?

For example - I would like to display my GRUB file in a terminal in Ubuntu 9.04 (cat /boot/grub/menu.lst) but don't want to display the comments(#).
 
Old 11-17-2009, 10:59 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
awk, grep are 2 of them. read their docs and try to code them yourself first.
 
Old 11-17-2009, 11:20 PM   #3
Thelionroars
LQ Newbie
 
Registered: Aug 2009
Location: Melbourne, Australia
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 1
<ghostdog74 thrusts fishing rod into thelionroars' hands>

Ok ok... a quick scan of the grep manpage showed me what I wanted (use the -v option). So:
Code:
cat /boot/grub/menu.lst | grep -v '#'
Thanks. Would your username be a reference to the film?
 
Old 11-17-2009, 11:51 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Thelionroars View Post
So:
Code:
cat /boot/grub/menu.lst | grep -v '#'
good effort. note: no need cat. Its useless. many i have come across always do that. Just pass the file to grep. Next, you may have "#" as comment at the back of the line. if you do -v "#" , then you may omit an actual line.

Quote:
Would your username be a reference to the film?
yes.
 
Old 11-18-2009, 06:53 PM   #5
Thelionroars
LQ Newbie
 
Registered: Aug 2009
Location: Melbourne, Australia
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by ghostdog74 View Post
note: no need cat. Its useless. many i have come across always do that. Just pass the file to grep.
Maybe its the Ubuntu permissions system, but the command won't work without cat, even with sudo. Without sudo, permission denied. With sudo, command not found. Maybe its different using a shell owned by root?

Quote:
Next, you may have "#" as comment at the back of the line. if you do -v "#" , then you may omit an actual line.
Good point. So I could use a caret (^) to specify that grep only selects lines that begin with the string -

cat /boot/grub/menu.lst | grep ^#

The material on regular expressions that I'm looking at also states that you can add an OR operator with | , but I'm not sure how to use this with grep?
 
Old 11-18-2009, 06:56 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,241

Rep: Reputation: 189Reputation: 189
you can make it pause so you an actually read it by replacing cat with more. just a tip.
 
Old 11-18-2009, 07:43 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
i would use gawk instead

Code:
awk '!/^ +#/' file  # one or spaces in beginning, don't print
 
Old 11-18-2009, 07:51 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,241

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by ghostdog74 View Post
i would use gawk instead

Code:
awk '!/^ +#/' file  # one or spaces in beginning, don't print
the grep version looked cleaner.
 
Old 11-18-2009, 07:58 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by smeezekitty View Post
the grep version looked cleaner.
and so? awk can do a whole lot more.
 
Old 11-18-2009, 09:42 PM   #10
Thelionroars
LQ Newbie
 
Registered: Aug 2009
Location: Melbourne, Australia
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 1
I see what you mean now by not needing cat - you give the file name as an argument to grep or awk and it goes to standard output, which by default is printing to the terminal. So to correct my grep usage -

grep -v ^# /boot/grub/menu.lst

No cat or pipes needed.
 
Old 01-21-2010, 09:11 AM   #11
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 48

Rep: Reputation: 16
As I recently learned a little about grep command and regular expressions, I would like to improve a bit the command above.
You can also skip the blank lines with
Code:
grep -E -v '^#|^ *$' <file>
The | character is a kind of 'and' operator and the string ^ *$ matches blank lines. At last, if I am right, the 'and' operator works just if the -E option is included.
 
Old 01-21-2010, 06:42 PM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Actually, '|' is the 'OR' operator
 
Old 01-22-2010, 08:03 AM   #13
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 48

Rep: Reputation: 16
Quote:
Originally Posted by chrism01 View Post
Actually, '|' is the 'OR' operator
Yes, that is true, actually I said 'and' without thinking enough.
 
  


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
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 04:46 PM
search for a string spread in multiple lines finder255 Linux - Software 2 10-22-2008 04:16 PM
unable to insert string on particular lines using sed livetoday Linux - Newbie 5 03-31-2008 12:28 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-21-2007 12:14 AM
Divide up lines with string delimiter elmu Programming 3 10-07-2005 09:48 AM


All times are GMT -5. The time now is 09:02 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration