LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 08-18-2010, 11:57 PM   #1
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Rep: Reputation: 25
Removing blank lines and commentary from a config file


Hi guys

I am currently using a command like this to remove blank lines and lines which contain (not necessarily begin) with a #.

Is there a better/simpler command?

cat /etc/apache2/default-server.conf | sed /^$/d | grep -v '#'

Thanks

rdc
 
Old 08-19-2010, 12:03 AM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
No need to use 'cat' and pipe to 'grep' or 'sed'.
 
Old 08-19-2010, 12:05 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

You can just use sed 'in place' on a file like this:
Code:
sed -i -e '/^$/d' -e '/^\#/d' /etc/apache2/default-server.conf
That will remove all blank lines and comments from the file.

Kind regards,

Eric
 
Old 08-19-2010, 12:18 AM   #4
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 25
Thanks everyone

This is the simplest:

Code:
sed '/^$/d' /etc/apache2/default-server.conf
Not quite sure why it is deleting lines which contain a # but it works.

Eric, unfortunately the command you suggested returned a null result! :-(
 
Old 08-19-2010, 12:21 AM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

On what distro are you running the command I provided? I tested it on Debian Squeeze and works like clockwork. Depending on the distro there might be differences. You also say that the command you used deletes the lines containing '#'? That's even more strange in my opinion. What if you only want to delete the blank lines but not the comments?

Kind regards,

Eric

Last edited by EricTRA; 08-19-2010 at 12:22 AM.
 
Old 08-19-2010, 12:24 AM   #6
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 25
openSuSE 11.3


Code:
thanatos:~ # sed -i -e '/^$/d' -e '/^\#/d' /etc/apache2/default-server.conf
thanatos:~ # echo $?
0
I'll try and figure out -i and -e later, couldn't quite figure it out from the man pages - unless you're willing to provide a quick explanation? :-)
 
Old 08-19-2010, 12:36 AM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

From the sed man page
Code:
-e script, --expression=script
-i[SUFFIX], --in-place[=SUFFIX]
           edit files in place (makes backup if extension supplied)
I just retested the command I provided and confirm that it does what it needs to do. Furthermore when I run
Code:
echo $?
I also get 0 returned.

If I only execute the command like this:
Code:
sed -i '/^$/d' testfile
I don't have to specify the -e parameter, it's only used with more then one 'command' or expression. If the -i option doesn't work for you (for example it doesn't in AIX), then you could try:
Code:
sed -e '/^$/d' -e '/^\#/d' /etc/apache2/default-server.conf > destinationfile
Kind regards,

Eric
 
Old 08-19-2010, 12:40 AM   #8
Leslie007
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 2
You can use these below commands,

=> sed '/^$/d;/^#/d' FileName
=> grep -v "^$\|^#" FileName
 
Old 08-19-2010, 12:43 AM   #9
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by Leslie007 View Post
You can use these below commands,

=> sed '/^$/d;/^#/d' FileName
=> grep -v "^$\|^#" FileName
Even better! Thanks Leslie007.

Kind regards,

Eric
 
Old 08-19-2010, 12:49 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Not that it looks much cleaner, but you can do it all in one:
Code:
sed -i -r '/^(#.*)*$/d' file
 
Old 08-19-2010, 02:22 AM   #11
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 25
Thanks guys,

Code:
sed -i -r '/^(#.*)*$/d' /etc/apache2/default-server.conf
That doesn't work on an openSuSE 11.3 system

What works so far is:

Code:
sed '/^$/d' /etc/apache2/default-server.conf
I'll mark this as solved as I achieved my objective, a simpler/better command to remove commentary and blank lines from a config file.
 
Old 08-19-2010, 02:24 AM   #12
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

You're welcome, glad you found what you wanted.

Kind regards,

Eric
 
Old 08-19-2010, 03:50 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
I must say I am still really curious how that removes lines starting with a #
 
Old 08-19-2010, 03:53 AM   #14
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 25
Quote:
I must say I am still really curious how that removes lines starting with a #
Indeed mate! That should only remove blank lines ...
 
Old 08-19-2010, 04:05 AM   #15
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

As stated in my post (#5). Still confused .

Kind regards,

Eric
 
  


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
Vim: How to hide commentary lines brianmcgee Programming 1 04-14-2008 09:15 AM
removing blank lines in a text file christianunix Linux - Newbie 11 10-29-2007 12:24 AM
Removing new lines from a file psandeepnair1985 Programming 5 03-25-2007 11:46 AM
How do i remove blank lines from a file? kakho Programming 1 04-15-2004 03:57 AM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 04:49 PM


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