LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 01-06-2005, 02:10 PM   #1
wujee
LQ Newbie
 
Registered: Jan 2005
Posts: 2

Rep: Reputation: 0
Angry Help, help! How to find lines in between searched patterns?


Dear all,

I have a html file and need to find the number of lines are comments. For example, this html file has comments begin from /* to */, which has total of 15 lines, like this,

/*
The following code looks for 3 different values:
nameN
phoneN
emailN
where 'N' is a number.

At the time of this coding the table looked like this:
PARAM_NAME PARAM_VALUE
---------- -----------
email1 k@yahoo.com
email2 eyahoo.com
name1 A John
name2 S John
*/

Besides, finding the number of lines of the comments and I also need to remove all the comments from the file. But I am not sure how to do that. As far as my understanding the grep and sed will only search for one line at a line and not the sequence of lines bettween patterns, like /* and */. I tried to use
sed -e '/^\/\*$/d' -e '/^\*\/$/d' command, but it only deletes the first and the last lines of the comments.

This is urgent, help please!

Thanks,
Wujee
 
Old 01-06-2005, 03:52 PM   #2
jeickal
Member
 
Registered: Oct 2004
Distribution: Mandrake, DSL, SuSE, RedHat
Posts: 71

Rep: Reputation: 15
I suggest you use a "while" loop and input your html file to it.
The loop will read line after line and output each line to an output_file. When /* is detected, then trigger a boolean variable to true (meaning you are in a comment section), you can start incrementing the number of lines counter. Keep looping until you detect a */. Then turn the boolean varaible back to false (no more in a comment area) stop increment the number of lines counter a re-start outputing the line to the output file.
You can write a script like this one, where:
input_file is your HTML file
output file will be you HTML without the stuff between /* and */
line_is_comment the boolean variable
nb_of_comment_line, I think you can figure
It will print at the end the number of comment line.
Don't forget to "chmod a+x" your script to make it executable.
If you launch it several time, you will have to remote "output_file" between each execution (otherwirse, lines will keep on adding up at the end of the file)

line_is_comment=false
declare -i nb_of_comment_line=0
while read PARAM_NAME PARAM_VALUE
do
if [ "$PARAM_NAME" = "/*" ]
then
line_is_comment=true
fi
if [ "$line_is_comment" = "false" ]
then
echo "$PARAM_NAME $PARAM_VALUE">>output_file
else
nb_of_comment_line=$nb_of_comment_line+1
fi
if [ "$PARAM_NAME" = "*/" ]
then
line_is_comment=false
fi
done<input_file
echo Nomber of comment lines is: $nb_of_comment_line

Note that this is bash script.

Hope this help. Can provide more detail if needed
 
  


Reply


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
linux find to find files with multiple patterns subu_s Programming 6 12-15-2010 01:15 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 06:18 PM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 04:01 PM
NVIDIA Searched... didnt find my problem linuxpupil Linux - Software 9 01-07-2004 09:27 PM
Can you find the error ? (160 lines) math-cases etc Dimitris Programming 5 09-02-2003 05:42 PM


All times are GMT -5. The time now is 02:38 PM.

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