regex - Trace Words in Shell Script -


i trying find malloc-free series. consider have 1000s of test cases. people may forget free ptr. please me in optimizing script.

sample file under test

test(func_class,func1) {   int i,j;   char* ptr = (char*) malloc(sizeof(char));   free(ptr); }  test(func_class,func1) {   int i,j;   char* ptr = (char*) malloc(sizeof(char));   / memory leak /  } 

script under development :

export my_root=`pwd` _count=0 pwd _count_word=0 filename=test.c cat $filename | while read line      echo "reading line = $line"      word in $line         _count_word=$(($_count_word+1))     echo $_count_word $word         if [ "$word" == "malloc\(sizeof\(char\)\);" ];             _malloc_flag=1   #this part of code not reached         echo "malloc flag hi"         echo $word[2]     fi     done     _count_word=0 done  

i have issue in matching malloc regex. know script needs lot of modifications have find pattern of individual person writing malloc.

there other ways check:

using awk:

awk '/malloc/ || /free/{count++;}end{printf "%d",count}' fileundertest 

this search "malloc" , "free" words in file , print out count. if count can every malloc there free.

using grep:

grep -c "malloc" fileundertest  

this count malloc word in file

similarly,

grep -c "free" fileundertest 

to list out line number

grep -n "malloc" fileundertest 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -