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
# 1  
Old 11-20-2014
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 line number and script line of all lines including comments. Normally this wouldn't be difficult, but I'm not supposed to use sed, awk, grep, etc (only Bash). I have the skeleton in place, but I'm missing how to pull only the comments and display the line number. Any help would be appreciated.

Code:
#!bin/bash

while read line
do
echo $line
done

# 2  
Old 11-20-2014
Quote:
Originally Posted by ksmarine1980
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 line number and script line of all lines including comments. Normally this wouldn't be difficult, but I'm not supposed to use sed, awk, grep, etc (only Bash). I have the skeleton in place, but I'm missing how to pull only the comments and display the line number. Any help would be appreciated.

Code:
#!bin/bash
 
while read line
do
echo $line
done

Hello ksmarine1980,

Following may help you in same if we need to print line number along with line's content.

Code:
i- awk '{print NR OFS $0}' Input_file
ii- cat -n Input_file
iii- while read line
    do
    let "i = i + 1"
    echo $i " " $line
    done < "Input_file"


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-20-2014
Thank you, Singh! But I'm trying to do this without the use of sed, awk, grep, etc.
# 4  
Old 11-20-2014
Hello ksmarine1980,

Not sure if I am correct, but I have given a while loop solution also in my previous post. Could you please let me know if that helps or you need anything else here.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 11-20-2014
Thank you Singh! I misread your post. I'll give it a shot, and appreciate your help.
# 6  
Old 11-20-2014
In command arguments have variables in quotes!
Code:
echo "$line"

Code:
echo "$i   $line"

Other suggestions:
Code:
printf "%05d %s\n" $i "$line"

Code:
read -r ...

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 11-20-2014
For the purposes of this exercise, how do you define "comment"?

A basic outline of what you want is something like:
Code:
#!/bin/bash
n=1
while read -r line
do      if [ comment_present ]
        then    printf '%d\t%s\n' $n "$line"
        fi
        n=$((n + 1))
done < file

If you assume that any line containing an octothorpe (#) has a comment, comment_present could be:
Code:
"$line" != "${line#*#}"

but, of course, that will treat quoted and backslash escaped octothorpes as comments when bash would not.

Writing a full parser to find C or shell or some other language's comments accurately in bash is a non-trivial challenge.

Last edited by Don Cragun; 11-20-2014 at 03:38 AM.. Reason: Print line number and line; not just line number.
This User Gave Thanks to Don Cragun For This Post:
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