Created
January 11, 2010 21:09
-
-
Save abhiomkar/274606 to your computer and use it in GitHub Desktop.
WC USING AWK - WITHOUT USING WC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author : Abhinay Omkar (c) 2007 | |
# Description : wc using awk. without using wc. | |
# License : GPLv2 | |
# This is free software. You may redistribute copies of it under the terms of | |
# the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. | |
# There is NO WARRANTY, to the extent permitted by law. | |
# | |
# Copyright (C) 2007 Free Software Foundation, Inc. | |
case $1 in | |
-*) | |
OPTION=$1 | |
FILENAME=$2 | |
;; | |
*) | |
FILENAME=$1 | |
OPTION=$2 | |
;; | |
esac | |
#Count number of lines: | |
#awk -F '\n' 'BEGIN {n=0} {++n} END {print n}' dmsg | |
#Count number of chars: | |
#awk -F '\n' 'BEGIN {c=0} {if(c==0) c=length($1); else c+=length($1)} END {print c}' dmsg | |
#+ | |
#awk -F '\n' 'BEGIN {b=0} /^$/ {++b} END {print b}' dmsg | |
#+ | |
#awk -F '\n' 'BEGIN {nb=0} !/^$/ {++nb} END {print nb}' dmsg | |
#Count number of words: | |
#awk 'BEGIN {w=0} {OFS="[:blank:]+"; if(w==0) w=NF; else w+=NF} END {print w}' dmsg | |
#Longest line in a file: | |
#awk -F '\n' 'BEGIN {L=0} {if(length($1)>L) L=length($1)} END {print L}' dmsg | |
case $OPTION in | |
-l|--lines) | |
echo `awk -F '\n' 'BEGIN {n=0} {++n} END {print n}' $FILENAME` $FILENAME | |
;; | |
-c|-m|--chars|--bytes) | |
echo `expr \`awk -F '\n' 'BEGIN {c=0} {if(c==0) c=length($1); else c+=length($1)} END {print c}' $FILENAME\` + \`awk -F '\n' 'BEGIN {b=0} /^$/ {++b} END {print b}' $FILENAME\` + \`awk -F '\n' 'BEGIN {nb=0} !/^$/ {++nb} END {print nb}' $FILENAME\`` $FILENAME | |
;; | |
-w|--words) | |
echo `awk 'BEGIN {w=0} {OFS="[:blank:]+"; if(w==0) w=NF; else w+=NF} END {print w}' $FILENAME` $FILENAME | |
;; | |
-L|--max-line-length) | |
echo `awk -F '\n' 'BEGIN {L=0} {if(length($1)>L) L=length($1)} END {print L}' $FILENAME` $FILENAME | |
;; | |
-v|--version) | |
echo "wc using awk. without using wc. | |
This is free software. You may redistribute copies of it under the terms of | |
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. | |
There is NO WARRANTY, to the extent permitted by law. | |
Copyright (C) 2007 Free Software Foundation, Inc. | |
Written by Abhinay Omkar" | |
;; | |
*) | |
echo "Written by Abhinay Omkar | |
Usage: awc [OPTION]... [FILE]... | |
Print newline, word, and byte counts for a FILE | |
c, --bytes print the byte counts | |
-m, --chars print the character counts | |
-l, --lines print the newline counts | |
-L, --max-line-length print the length of the longest line | |
-w, --words print the word counts | |
--help display this help and exit | |
--version output version information and exit" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment