I wrote a rough script for counting the number of lines in C language code. There are still some bugs and the efficiency is not high. The script removes most comments and counts the number of rows, which is equivalent to preprocessing. The following code is used:
#! /Bin/bash
Filename = $1
Echo "'whoam '"
If [$ #-lt 1]; then
Echo "usage:./scripts filename"
Exit-1
Fi
If [! -F $ filename]; then
Echo "$ filename is not a file"
Exit 0;
Fi
User = "'whoam '"
If ["$ user "! = "Root"]; then
Echo "use scripts with root"
Exit 0;
Fi
# Delete empty rows, including tables + space
Sed '/^ [[: blank:] * $/d' $ filename> "$ {filename} _ backup"
# Delete the // row
Sed-I '/^ [[: blank:] * \/\ // d' "$ {filename} _ backup"
#/* Xxxxx */
# Print/* all start rows
Grep-n' [[: blank:] */[*] '"$ {filename} _ backup" | awk' BEGIN {FS = ": "} {print $1} '>" start.txt"
# Print */all end rows
Grep-n' [*]/[[: blank:] * '"$ {filename} _ backup" | awk 'begin {FS = ": "} {print $1} '>" end.txt"
Merge two files, and each line in final.txt is the start line and start line of the/* xx... \ n... xx */annotation of this type.
Paste "start.txt" "end.txt"> "final.txt"
While read line
Do
# Obtain the starting line
START = 'echo "$ line" | awk 'in in {FS = "\ t"} {print $1 }''
# Get the end line
END = 'echo "$ line" | awk 'in in {FS = "\ t"} {print $2 }''
# Here is a bug, such as/* xxxx */and */In the same line, and some code is in the same line
# Printf ("hello world \ n");/* print the string */
#/* Print the string */
# The above two situations cannot be determined. I hope you can optimize them.
If [$ START-eq $ END]; then
Continue
Fi
# Delete/* to */all rows in the middle
Sed-I "$ {START}, $ {END} d" "$ {filename} _ backup"
Done <"final.txt"
Wc-l "$ {filename} _ backup"
Rm-f "final.txt" "start.txt" "end.txt"
Author: firefoxbug