Return to Snippet

Revision: 3128
at June 6, 2007 01:42 by abhiomkar


Updated Code
#!/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

Revision: 3127
at June 6, 2007 01:37 by abhiomkar


Updated Code
#!/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}' dmsg` $FILENAME
	;;
   -l|--max-line-length)
	echo `awk -F '\n' 'BEGIN {L=0} {if(length($1)>L) L=length($1)} END {print L}'` $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

Revision: 3126
at June 6, 2007 01:01 by abhiomkar


Initial Code
#!/bin/bash

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

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
        ;;
   *)
        echo "Usage: awc [-l][-c] filename"
        exit 1
        ;;
esac

Initial URL


Initial Description
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"

Initial Title
Implementing wc using awk (without using wc)

Initial Tags


Initial Language
Bash