Bash Implementation of C language code line statistics

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.