Bash script to find comments in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to find comments in file
# 8  
Old 11-20-2014
Thank you everyone for your help. I want to read in the from from the command line, search for comments, then output to the screen: example ./myscript.bash <comments_script.sh. If no comments are found in the script, then I simply want to output "No comments found."

Here's what I have so far, which prompts me for more info. I know it's a simple exercise for most of you, but for my first month of programming it's a challenge for me.

Code:
n=1
while read -r line
do      if [ "$line" != "${line#*#}" ]
        then
                printf '%d\t%s\n' $n "$line"
        else
                echo "No comments found"
        fi
        n=$((n + 1))
done

# 9  
Old 11-20-2014
Don't tell us it "prompts me for more info"; show us the exact message that bash prints as a prompt.

When I save the following:
Code:
#!/bin/bash
found=0
n=1
while read -r line
do	# Look for comments...
	if [ "$line" != "${line#*#}" ]
	then	# Comment found, print line with line #...
		printf '%d\t%s\n' $n "$line"
		# Note that we found a comment...
		found=1
	fi
	# Increment line # for next line...
	n=$((n + 1))
done
# Print note if no comments found...
if [ ! $found ]
then	echo "No comments found."
fi

in myscript.bash, make it exeutable with:
Code:
chmod +x myscript.bash

and execute it with:
Code:
./myscript.bash < myscript.bash

(which reads itself as a file; not from data typed into the shell on standard input), I get the output:
Code:
1	#!/bin/bash
5	do	# Look for comments...
6	if [ "$line" != "${line#*#}" ]
7	then	# Comment found, print line with line #...
9	# Note that we found a comment...
12	# Increment line # for next line...
15	# Print note if no comments found.

which correctly shows all lines with comments, but has a false positive on line #6. It does not prompt for any more info.

As I said before: "Writing a full parser to find C or shell or some other language's comments accurately in bash is a non-trivial challenge." I'm not going to attempt to weed out the false positives this script will print for you on a forum like this.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 11-20-2014
That's perfect, Don!

The only line I'm not sure I understand:
if [ "$line" != "${line#*#}" ]

I understand the reference to line from above, and the not equal (!=), but the #*# is confusing. I want to make sure I understand everything I'm doing as well. Thanks again!
# 11  
Old 11-20-2014
Don uses string substitution feature of bash, you can
Code:
man bash

and read all the details

To demonstrate:

Code:
  
$ a="123 abc # qwe"
$ echo ${a#*#}     
qwe
  
$ a="123 abc + qwe"
$ echo ${a#*#}     
 123 abc + qwe

So, when your line does not have '#' the if statement will evaluate it as equal and not go into reporting a comment
This User Gave Thanks to migurus For This Post:
# 12  
Old 11-20-2014
Thank you kind sir! And thanks again to all who helped

Last edited by ksmarine1980; 11-20-2014 at 10:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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. #!/bin/bash perl -pe 's/ *#.*$//g' $1 | grep -v ^]*$ | perl -pe 's/ +/ /g' > $2 # # $1 INFILE # $2 OUTFILE The above code... (10 Replies)
Discussion started by: LessNux
10 Replies

2. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

3. Shell Programming and Scripting

BASH script problem using find, ideas?

Hi, I'm trying to write a script to search through my computer and find all .jpg files and put them all in a directory. So far I have this: for i in `find /home -name '*.jpg' ` ; do mv $i home/allen/Pictures/PicturesFound ; done When I run it, I get this error (this is only part of it, it... (2 Replies)
Discussion started by: FortressPTH
2 Replies

4. Programming

Bash Script to Find the status of URL

#!/bin/bash timevar=`date +%F_”%H_%M”` #-- > Storing Date and Time in a Variable get_contents=`cat urls.txt` #-- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of ######### Next Section Does all the processing ######### for i... (0 Replies)
Discussion started by: anishkumarv
0 Replies

5. Shell Programming and Scripting

indication of find activity - bash script

Hi All, I wanted to show on stdout that a file was found right after it happens due to indicate the activity of long search. Further more I want to store the result of the find in a file. I have tried this: echo -n "Searching" find . -name Makefile -type f -print -exec echo -n "." \; >... (16 Replies)
Discussion started by: vercsab
16 Replies

6. Shell Programming and Scripting

Find out the day in Bash Shell script

Hello All, I need a bash shell script to find out a day from the date.For example we give the date(20100227/YYYYMMDD) then we get the day 'Saturday'. Thanks in advance, Satheesh (5 Replies)
Discussion started by: satheesh4093
5 Replies

7. 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

8. Shell Programming and Scripting

Bash script (using find and grep)

I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n). #!/bin/bash SEARCHQUERY="searchword1 searchword2 searchword3"; for WORD in $SEARCHQUERY do ... (6 Replies)
Discussion started by: limmer
6 Replies

9. 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

10. Shell Programming and Scripting

Request for comments -- Directory favorites under bash

Here are a couple of files related to some ideas I have had on providing a mechanism for navigation across a set of "favorite directories." I would appreciate any comment on the approach and any other useful recommendations. Please visit my project home page on sourceforge Bash Navigator Home... (0 Replies)
Discussion started by: rlandon@usa.net
0 Replies
Login or Register to Ask a Question