LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-07-2004, 02:09 AM   #1
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Question how to remove all commented lines


what's the easiest, command-line way to remove all the commented lines in a shell script???
 
Old 05-07-2004, 02:32 AM   #2
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
ack, not thinking....hold that thought.

For some reason, I'm pulling a brainfart on this...it's something I've done a few times, but now I can't remember how *sigh*, getting old can sometimes suck.

Ok, because, for the life of me, I can't remember how to do this in vi, here's a quick kludge. Assuming that, like most shells scripts, comments start out with a # as the first thing on the line, you can do this.

cat some_file | grep -v ^# > some_new_file

If someone can kickstart my brain and post the vi equivalent for this, I'd appreciate it.

Last edited by Technoslave; 05-07-2004 at 02:42 AM.
 
Old 05-07-2004, 11:43 AM   #3
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
This should do the trick (for vi, I mean)

:g/^#/d

The "easiest" command-line way might be:

grep -v ^# some_file

Arvind

Last edited by arvind_sv; 05-07-2004 at 12:18 PM.
 
Old 05-07-2004, 11:51 AM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
thanks so much technoslave!!!

=)



i did this, as you suggested:


cat /etc/squid/squid.conf | grep -v ^# > /etc/squid/squid.conf.stripped


and it seems to have worked perfectly...

but now how would i go about removing the blank lines so that the remaining text is tightly arranged???



Last edited by win32sux; 05-07-2004 at 11:59 AM.
 
Old 05-07-2004, 11:54 AM   #5
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
But at the same time you're removing the first line, which is an important part of the script.
You may want to do something like:
Code:
echo "#!/bin/sh" > some_file; cat script_file | grep -v ^# >> some_file
 
Old 05-07-2004, 11:54 AM   #6
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally posted by arvind_sv
This should do the trick (for vi, I mean)

:g/^#/d

thanks!!!
 
Old 05-07-2004, 11:58 AM   #7
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally posted by mikshaw
But at the same time you're removing the first line, which is an important part of the script.
thanks, good call.. i'd forgotten about the shebang, since i was trying this out on /etc/squid/squid.conf, which uses "#" for comments also but isn't a shell script, hence no shebang...
 
Old 05-07-2004, 01:55 PM   #8
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
okay i think i found a way to remove the blank lines...

this (example) would make it so that there is no more than one blank line at a time (".stripped" means the comments have already been removed using cat and grep):


cat -s /etc/squid/squid.conf.stripped > /etc/squid/squid.conf.stripped.oneblank



and it seems this would get rid of the blank lines entirely (i googled a bit and found this here):


cat -s /etc/squid/squid.conf.stripped | sed '/^[[:space:]]*$/d' > /etc/squid/squid.conf.stripped.noblanks



do you know of a simpler way to do it, or is this pretty much it??

i'm wondering if "sed" is supposed to be a "standard" part of GNU (as i believe cat and grep to be), so as one could expect every distro to have sed...???

sorry for being such a newbie...

thanks so much for your input, i really appreciate it...


Last edited by win32sux; 05-07-2004 at 02:03 PM.
 
Old 05-07-2004, 08:21 PM   #9
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
You want a way to get rid of all blank lines? This will do it:
tr -s "\n" </etc/squid/squid.conf.stripped

But, why do you want to struggle so much? How about this:
grep -v "^#" squid.conf | grep -v "^$" >/etc/squid/squid.conf.stripped.noblanks

Or even,
egrep -v "^#|^$" squid.conf >/etc/squid/squid.conf.stripped.noblanks

This would get rid of the temporary "stripped" file.

Don't get into the habit of using "cat" all over. Most commands take a filename or input can be piped in using "<", which will do what cat will, sometimes more efficiently. Look at: http://www.faqs.org/docs/jargon/U/UUOC.html or even http://rhols66.adsl.netsonic.fi/era/unix/award.html

Arvind
 
Old 05-07-2004, 09:27 PM   #10
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally posted by arvind_sv

grep -v "^#" squid.conf | grep -v "^$" >/etc/squid/squid.conf.stripped.noblanks

egrep -v "^#|^$" squid.conf >/etc/squid/squid.conf.stripped.noblanks
awesome!!! thanks!!!

=)



Quote:
You want a way to get rid of all blank lines? This will do it:
tr -s "\n" </etc/squid/squid.conf.stripped
the "greps" from above do everything i need, but i'm curious about this "tr" cuz i can't seem to make it work...

after running it the file remains exactly the same (md5sum)...



Quote:
Don't get into the habit of using "cat" all over. Most commands take a filename or input can be piped in using "<", which will do what cat will, sometimes more efficiently. Look at: http://www.faqs.org/docs/jargon/U/UUOC.html or even http://rhols66.adsl.netsonic.fi/era/unix/award.html
whoa... thanks a million for that very good advice, and those very informative links...

"Useless Use of Cat Award"... lol... that was cool...

=)
 
Old 05-07-2004, 10:29 PM   #11
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Thanks from here too....I'd seen that in a few scripts but never understood its purpose
 
Old 05-08-2004, 03:27 AM   #12
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
You're both welcome.

tr (short for translate) is just a filter. It's a filter in the truest sense of the word. It just filters it's input and writes it to stdout. What you want is probably something like:

Code:
tr -s "\n" </etc/squid/squid.conf.stripped >/etc/squid/squid.conf.stripped.noblanks
It will not change the input file as it has no idea about it (that is, you're just piping the stripped file into it).

Arvind
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove the first part of lines? daYz Linux - Software 3 11-26-2005 06:09 PM
How to remove lines from a file doza Linux - General 2 04-27-2005 11:59 AM
/etc/securetty --> I commented out all lines and I can still log in as root adamrau Linux - Security 2 05-30-2004 06:16 AM
[bash] remove lines from a file Drimo Programming 3 03-20-2004 11:16 AM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:59 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