Sponsored Content
Top Forums Shell Programming and Scripting How to remove comments from a bash script? Post 302660261 by LessNux on Friday 22nd of June 2012 09:21:20 AM
Old 06-22-2012
Question How to remove comments from a bash script?

I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines.

Code:
#!/bin/bash
perl -pe 's/ *#.*$//g' $1 | grep -v ^[[:space:]]*$ | perl -pe 's/  +/ /g' > $2
#
# $1 INFILE
# $2 OUTFILE


The above code seemed to work at first. Unfortunately, however, I found later that the above code destroys the following two special variables.

${#ARRAY[@]} the number of array elements
$# the number of shell arguments

A workaround is to replace "${#" and "$#" with words that do not appear in the input file before applying the above code.


Code:
sed 's/${#/__UNUSUALWORD1__/g ; s/$#/__UNUSUALWORD2__/g' in.txt | \
perl -pe 's/ *#.*$//g' | grep -v ^[[:space:]]*$ | perl -pe 's/  +/ /g' | \
sed 's/__UNUSUALWORD1__/${#/ ; s/__UNUSUALWORD2__/$#/' > out.txt


However, the preparatory replacement is awkward. I would like to modify 's/ *#.*$//g' so that it will not match "${#" or "$#". Does anyone know a better solution?

Bash comments always start with #. However, the problem is that bash allows some exceptions where # does not lead a comment, as shown below.

Code:
${#ARRAY[@]} the number of array elements
$# the number of shell arguments
\# escaped by a backslash.
'abcd#efgh' protected by quotes
"abcd#efgh" protected by quotes

Does anyone know how to remove comments from bash scripts without destroying the exempted #'s that do not lead comments? (In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines.)

Many thanks in advance.

Last edited by methyl; 06-22-2012 at 11:33 AM.. Reason: more code tags
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove comments...

It may be a no-brainer, but the answer is escaping me right now: I'm trying to write a little script to remove all comments from .c source... I was thinking sed, but I'm not a very strong regexp user (e.g. I suck with sed). I tried dumping the file into: sed -e 's/\/\* * \*\///g' and several... (1 Reply)
Discussion started by: LivinFree
1 Replies

2. Shell Programming and Scripting

please explain this sed shell script to remove C++ comments.

#! /bin/sed -nf # Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com) # Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org) # Works its way through the line, copying to hold space the text up to the # first special character (/, ", '). The original... (1 Reply)
Discussion started by: Priyaranjan
1 Replies

3. Shell Programming and Scripting

how can i remove comments in random positions in a file?(bash)

Suppose i have a file like this: #bla bla #bla bla bla bla bla Bla BLA BLA BLA #bla bla .... .... how can i remove all comments from every line,even if they are behind commands or strngs that are not comments? any idea how i could do that using awk? (2 Replies)
Discussion started by: bashuser2
2 Replies

4. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

5. UNIX for Dummies Questions & Answers

Remove blank lines and comments from text file

Hi, I am using BASH. How can I remove any lines in a text file that are either blank or begin with a # (ie. comments)? Thanks in advance. Mike (3 Replies)
Discussion started by: msb65
3 Replies

6. Shell Programming and Scripting

sed remove css comments

Is there a way that I can use sed to remove lines with css comments like this? /* comment */ (9 Replies)
Discussion started by: gravesit
9 Replies

7. Shell Programming and Scripting

sed remove comments

I need to use sed to remove comments from files. I am using this, but it only works on comments that start at the beginning of the line. sed /^"\/\/"/d In most of the files I have comments like this: code // Comments or tab // Comments (5 Replies)
Discussion started by: gravesit
5 Replies

8. UNIX for Dummies Questions & Answers

Remove SAS comments using UNIX

I have tried a lot, Need your help guys. SAS Program: data one ; /* Data step */ Input name $; /*Dec variables*/ I want to remove the commented part(/* Data step */) alone. I have tried using sed command but it is deleting the entire line itself. i need unix command to separate this and... (1 Reply)
Discussion started by: saaisiva
1 Replies

9. Shell Programming and Scripting

Bash script to find comments in file

As I stated in a previous thread - I'm a newbie to Unix/Linux and programming. I'm trying to learn the basics on my own using a couple books and the exercises provided inside. I've reached an exercise that has me stumped. I need to write a bash script that will will read in a file and print the... (11 Replies)
Discussion started by: ksmarine1980
11 Replies

10. Shell Programming and Scripting

Remove comments like pattern from text

Hi , We need to remove comment like pattern from a code text. The possible comment expressions are as follows. Input BizComment : Special/*@ Name:bzt_53_3aea640a_51783afa_5d64_0 BizHidden:true @*/ /* lookup Disease Category Therapuetic Class */ a=b;... (6 Replies)
Discussion started by: VikashKumar
6 Replies
Text::ParseWords(3pm)					 Perl Programmers Reference Guide				     Text::ParseWords(3pm)

NAME
Text::ParseWords - parse text into an array of tokens or array of arrays SYNOPSIS
use Text::ParseWords; @lists = &nested_quotewords($delim, $keep, @lines); @words = &quotewords($delim, $keep, @lines); @words = &shellwords(@lines); @words = &parse_line($delim, $keep, $line); @words = &old_shellwords(@lines); # DEPRECATED! DESCRIPTION
The &nested_quotewords() and &quotewords() functions accept a delimiter (which can be a regular expression) and a list of lines and then breaks those lines up into a list of words ignoring delimiters that appear inside quotes. &quotewords() returns all of the tokens in a single long list, while &nested_quotewords() returns a list of token lists corresponding to the elements of @lines. &parse_line() does tokenizing on a single string. The &*quotewords() functions simply call &parse_lines(), so if you're only splitting one line you can call &parse_lines() directly and save a function call. The $keep argument is a boolean flag. If true, then the tokens are split on the specified delimiter, but all other characters (quotes, backslashes, etc.) are kept in the tokens. If $keep is false then the &*quotewords() functions remove all quotes and backslashes that are not themselves backslash-escaped or inside of single quotes (i.e., &quotewords() tries to interpret these characters just like the Bourne shell). NB: these semantics are significantly different from the original version of this module shipped with Perl 5.000 through 5.004. As an additional feature, $keep may be the keyword "delimiters" which causes the functions to preserve the delimiters in each string as tokens in the token lists, in addition to preserving quote and backslash characters. &shellwords() is written as a special case of &quotewords(), and it does token parsing with whitespace as a delimiter-- similar to most Unix shells. EXAMPLES
The sample program: use Text::ParseWords; @words = &quotewords('s+', 0, q{this is "a test" of quotewords "for you}); $i = 0; foreach (@words) { print "$i: <$_> "; $i++; } produces: 0: <this> 1: <is> 2: <a test> 3: <of quotewords> 4: <"for> 5: <you> demonstrating: 0 a simple word 1 multiple spaces are skipped because of our $delim 2 use of quotes to include a space in a word 3 use of a backslash to include a space in a word 4 use of a backslash to remove the special meaning of a double-quote 5 another simple word (note the lack of effect of the backslashed double-quote) Replacing "&quotewords('s+', 0, q{this is...})" with "&shellwords(q{this is...})" is a simpler way to accomplish the same thing. AUTHORS
Maintainer is Hal Pomeranz <pomeranz@netcom.com>, 1994-1997 (Original author unknown). Much of the code for &parse_line() (including the primary regexp) from Joerk Behrends <jbehrends@multimediaproduzenten.de>. Examples section another documentation provided by John Heidemann <johnh@ISI.EDU> Bug reports, patches, and nagging provided by lots of folks-- thanks everybody! Special thanks to Michael Schwern <schwern@envirolink.org> for assuring me that a &nested_quotewords() would be useful, and to Jeff Friedl <jfriedl@yahoo-inc.com> for telling me not to worry about error-checking (sort of-- you had to be there). perl v5.8.0 2002-06-01 Text::ParseWords(3pm)
All times are GMT -4. The time now is 01:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy