LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2012, 11:26 AM   #1
Rogue45
Member
 
Registered: Jun 2012
Posts: 47

Rep: Reputation: Disabled
Bash script to count lines of code?


I want to write a bash script to count the lines of code vs comments in a secondary bash script i have. Comments start with a # and can have spaces or tabs preceeding it. A simple grep -c returns the number of comments but doesn't give an accurate count of comment lines. How can i do this?
 
Old 11-26-2012, 11:31 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Sounds some home work... ain't it?
Commands such as wc will tell you number count, plus vi editor will also tell you total no. of lines using :se nu option in it's colon mode.
However mention that what you've tried so far.

Last edited by shivaa; 11-26-2012 at 11:33 AM.
 
Old 11-26-2012, 01:48 PM   #3
Rogue45
Member
 
Registered: Jun 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Not homework.

I've successfully gotten my line counts but its pretty awful.

I used a sed to identify comment lines easily
sed -i 's/#.*/removed/' root/Desktop/test.txt

then i removed blank lines
sed -i '/^$/d' /root/Desktop/test.txt

then i grep -c removed ,to get comment line count

then total lines - comment lines = actual loc

Anyways that method sucks so please show me a prettier method.
 
Old 11-26-2012, 04:16 PM   #4
steelneck
Member
 
Registered: Nov 2005
Distribution: Slackware, Arch
Posts: 43

Rep: Reputation: 8
I do not think it qualifies as pretty, but i think this script will work for you. Just feed it with the file you want to count lines in as argument.

Code:
#!/bin/sh
noblank=$(sed '/^$/d' $1)
echo "Lines with code: $(echo "$noblank" |  grep -cv "#")"
echo "Lines with comments: $(echo "$noblank" |  grep -c "#")"

Last edited by steelneck; 11-26-2012 at 04:17 PM.
 
1 members found this post helpful.
Old 11-27-2012, 06:35 AM   #5
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Counting the lines that begin with #.
grep -c '^#' script.sh

Just using grep '#' will also match these lines. That line is not a comment is it?

Code:
[root@beastlinux ~]# grep '#' script 
echo "product #5"
[root@beastlinux ~]# grep '^#' script 
[root@beastlinux ~]#
I have the script I helped another user with on the forum.

Code:
[root@beastlinux ~]# cat recycle 
#!/bin/bash

recycleBin=/tmp/recycle_bin

function recycle_files {
  mv -v "$1" $recycleBin
}

case "$1" in
  -r) recycle_files $2 ;;
   *) echo "-r wasn't used" ;;
esac
[root@beastlinux ~]#
This greps only the code. Any lines that are blank, contain only white space, or begin with a comment are removed from the results leaving only the lines of code.
Code:
[root@beastlinux ~]# egrep -v '^$|^#|^[  ]$' recycle 
recycleBin=/tmp/recycle_bin
function recycle_files {
  mv -v "$1" $recycleBin
}
case "$1" in
  -r) recycle_files $2 ;;
   *) echo "-r wasn't used" ;;
esac
[root@beastlinux ~]#
The [ ] contains a space and a tab character. To insert the tab, push control + v, then tab.

So if you want to count those different things, just add -c to grep.

Last edited by fakie_flip; 11-27-2012 at 06:38 AM.
 
1 members found this post helpful.
Old 11-27-2012, 07:59 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I must say I am a little concerned by all the code that is changing files when all we are looking for is information. I refer to both the use of moving files around or using sed to directly
changes files. Hopefully not to be used in a live environment I guess.

Anyhoo, how about something simple like:
Code:
awk '{if($1 ~ /^#/)comment++; else no_comment++}END{print "line of code =",no_comment,"and number of comments =",comment}' file
 
Old 11-27-2012, 04:59 PM   #7
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Quote:
Originally Posted by grail View Post
I must say I am a little concerned by all the code that is changing files when all we are looking for is information.
I am not suggesting this code be used. I was just using it as an example for counting lines with comments and counting lines of actual code excluding comment lines, white space lines, and blank lines. The code was something I fixed for another user on the forum. See:

https://www.linuxquestions.org/quest...ts-4175438882/
 
  


Reply



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
bash script to count number of lines with a specific property7 hhamid Programming 10 08-13-2010 01:35 AM
script to count lines kalimat Programming 16 12-15-2008 04:09 AM
Script to count lines from 'jobs' not working ... SlowCoder Linux - Newbie 5 05-02-2007 09:56 AM
Recursively count lines of code in source dir heirarchy michaelsanford Programming 8 04-01-2006 04:59 PM
How to count how many lines of code my project is? The_Nerd Programming 4 08-30-2004 08:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:53 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
Open Source Consulting | Domain Registration