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.
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.
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.
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
The problem is, to understand which # are comments and which # aren't, you have to understand the script. "#" '#' should not be stripped out for instance -- or any other time # is not given to the bare line but wrapped inside something. It's possible to wrap # inside quite complex structures, I'm not sure you can check for every possible thing with regexes alone.
What's the best thing out there for understanding scripts? A shell, of course.
There is an option for the shell to print lines as it executes them(which strips out comments). Also, an option to check lines for syntax without actually running them. Unfortunately they seem to be mutually exclusive. Seeing if there's anything else relevant...
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.
I'm curious. Why?
Regards,
Alister
---------- Post updated at 12:21 PM ---------- Previous update was at 12:11 PM ----------
Also, while your post makes it clear that you are aware that naively removing #.* can cause problems, the same is true for a blank line. If it is part of a heredoc or some other quoted string, removing a blank line alters the meaning of the script.
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)
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)
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)
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)
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)
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)
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)
#! /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)
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)