shell脚本统计C/C++代码行

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc #current directory CURR_DIR='.' if [ ! -z $1 ];then CURR_DIR=$1 fi # create 3 tempory file to record detail infomation find $CURR_DIR -name "*.cpp" | xargs wc -l > cpp_line_detail.txt find $CURR_DIR -name "*.c" | xargs wc -l > c_line_detail.txt find $CURR_DIR -name "*.h" | xargs wc -l > h_line_detail.txt #count CPP_COUNT=$(tail -1 cpp_line_detail.txt | sed -e 's/^ *//' | sed -e 's/ .*$//') C_COUNT=$(tail -1 c_line_detail.txt | sed -e 's/^ *//' | sed -e 's/ .*$//') H_COUNT=$(tail -1 h_line_detail.txt | sed -e 's/^ *//' | sed -e 's/ .*$//') echo "The code in *.cpp files is "$CPP_COUNT" lines" echo "The code in *.c files is "$C_COUNT" lines" echo "The code in *.h files is "$H_COUNT" lines" echo "------------------------------------" #the final result echo "The total number of lines is "$(($CPP_COUNT + $C_COUNT + $H_COUNT)) 后记 这是我2012年的文章 ...

January 19, 2018 · 1 min

shell 分割文件

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 帮同事写的小程序 140822,1406181801491716879,221.203.75.168,20140822000014 140823,1408051715321587060,101.28.174.242,20140822000127 140823,1408051715321587060,101.28.174.242,20140822000129 140824,1408051715321587060,101.28.174.242,20140822000139 140824,1406031640261808247,110.254.245.82,20140822000205 140825,1305230023521467300,210.73.6.180,20140822000216 140825,1408181402431171048,110.243.255.56,20140822000216 140825,1408131900341325654,110.248.233.239,20140822000216 140825,1407071756131811923,27.213.51.178,20140822000228 140826,1408171201311863011,124.67.26.134,20140822000238 某个数据文件的内容如上,每行的第一部分是时间140822 2014年8月22日 要按不同的日期分割文件, shell脚本如下: cat data.txt | while read line do if [ -n "$line" ] then echo $line echo ${line%%,*} echo $line >> "${line%%,*}.txt" echo "${line##*,}" fi done 实际运行中,速度非常差,无法容忍,由于时间的起止是从140822 至 140826 所以程序改为 date for((i=140818;i<=140826;i++));do echo $i; awk '{if(/^'$i'/)print $0;}' data.txt >$i.data.csv done; date 这样速度有了明显的提升,推断速度提升的原因是减少了IO操作次数,子进程创建和销毁的次数也减少了 尝试使用grep替代awk命令发现速度更快 date for((i=140818;i<=140826;i++));do echo $i; grep "$i," data.txt > "${i}.txt" done; date 使用awk命令耗时为6分钟左右,而使用grep 命令耗时大约为3分多钟

January 2, 2018 · 1 min