LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help, help! How to find lines in between searched patterns? (https://www.linuxquestions.org/questions/linux-software-2/help-help-how-to-find-lines-in-between-searched-patterns-274652/)

wujee 01-06-2005 01:10 PM

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

jeickal 01-06-2005 02:52 PM

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


All times are GMT -5. The time now is 10:26 PM.