Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 08-08-2006, 05:05 AM   #1
Registered: Jul 2006
Posts: 41

Rep: Reputation: 15
a file without comments

Hi..İs there an easy way of cleaning out the comments in a bash shell script file?(cleaning out the lines beginning with "#" may be even helpful enough)Thank u..
Old 08-08-2006, 05:08 AM   #2
LQ Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
I believe you can achieve this with cat and grep:
cat scriptfile | grep -v "#" > scriptfile_new
That reads the script file (scriptfile), then prints out every line except those with comment marks # and outputs it to scriptfile_new

EDIT: oh but I just took it off the top of my head so don't delete the original script before checking out you didn't miss anything.. i.e. if there are lines that contain the #-mark but that are NOT completely comments, those lines get drawn off totally anyway. Shortly said, every line containing # gets removed. So a line like

somecode(); # this does somecode

would get removed too, though the comment is only after the code piece (but still on the same line).

You might want something a bit more clever to wipe out comments that are on the same line than some non-comment code..maybe sed or some other alike tool could read the text and when it encounters a # it would not read text until the next linefeed..well, I'm not sure. Better keep comments at their own lines

Last edited by b0uncer; 08-08-2006 at 05:13 AM.
Old 08-08-2006, 06:20 AM   #3
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
1. cat some_file | grep ...
can be replaced by:
grep ... some_file

2. As indicated, the given grep won't do. It will also omit lines that start with code and end with a comment.
Rather, use a regular expression like this:
grep -E -v '^#' scriptfile
That will only throw out lines that start off with a '#'.

3. If you want to clean out comments from lines like
some_code; #some_comment
then you'll need to look at something else than grep. 'cut' and 'awk' seem to be good choices. I wouldn't recommend 'sed' for this one.
cut -d'#' -f1 scriptfile;
should read the file and keep only the text that's before the first '#'.


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
Your Comments, Please !! cousinlucky General 6 01-21-2006 08:28 PM
2.6.12 out, comments in :) xushi Slackware 107 07-12-2005 07:16 AM
Comments Please bigjohn General 9 11-16-2002 10:32 AM
Comments Please webboss Linux - General 2 11-15-2002 07:23 AM
application for file comments/descriptions Serena Linux - Software 1 05-01-2002 12:05 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:21 PM.

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