Implementing wc using awk (without using wc)


/ Published in: Bash
Save to your folder(s)

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"


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. # Author : Abhinay Omkar (c) 2007
  3. # Description : wc using awk. without using wc.
  4. # License : GPLv2
  5. # This is free software. You may redistribute copies of it under the terms of
  6. # the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
  7. # There is NO WARRANTY, to the extent permitted by law.
  8. #
  9. # Copyright (C) 2007 Free Software Foundation, Inc.
  10.  
  11. case $1 in
  12. -*)
  13. OPTION=$1
  14. FILENAME=$2
  15. ;;
  16. *)
  17. FILENAME=$1
  18. OPTION=$2
  19. ;;
  20. esac
  21.  
  22. #Count number of lines:
  23. #awk -F '\n' 'BEGIN {n=0} {++n} END {print n}' dmsg
  24.  
  25. #Count number of chars:
  26. #awk -F '\n' 'BEGIN {c=0} {if(c==0) c=length($1); else c+=length($1)} END {print c}' dmsg
  27. #+
  28. #awk -F '\n' 'BEGIN {b=0} /^$/ {++b} END {print b}' dmsg
  29. #+
  30. #awk -F '\n' 'BEGIN {nb=0} !/^$/ {++nb} END {print nb}' dmsg
  31.  
  32. #Count number of words:
  33. #awk 'BEGIN {w=0} {OFS="[:blank:]+"; if(w==0) w=NF; else w+=NF} END {print w}' dmsg
  34.  
  35. #Longest line in a file:
  36. #awk -F '\n' 'BEGIN {L=0} {if(length($1)>L) L=length($1)} END {print L}' dmsg
  37.  
  38. case $OPTION in
  39. -l|--lines)
  40. echo `awk -F '\n' 'BEGIN {n=0} {++n} END {print n}' $FILENAME` $FILENAME
  41. ;;
  42. -c|-m|--chars|--bytes)
  43. 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
  44. ;;
  45. -w|--words)
  46. echo `awk 'BEGIN {w=0} {OFS="[:blank:]+"; if(w==0) w=NF; else w+=NF} END {print w}' $FILENAME` $FILENAME
  47. ;;
  48. -L|--max-line-length)
  49. echo `awk -F '\n' 'BEGIN {L=0} {if(length($1)>L) L=length($1)} END {print L}' $FILENAME` $FILENAME
  50. ;;
  51. -v|--version)
  52. echo "wc using awk. without using wc.
  53. This is free software. You may redistribute copies of it under the terms of
  54. the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
  55. There is NO WARRANTY, to the extent permitted by law.
  56.  
  57. Copyright (C) 2007 Free Software Foundation, Inc.
  58. Written by Abhinay Omkar"
  59. ;;
  60. *)
  61. echo "Written by Abhinay Omkar
  62. Usage: awc [OPTION]... [FILE]...
  63. Print newline, word, and byte counts for a FILE
  64. c, --bytes print the byte counts
  65. -m, --chars print the character counts
  66. -l, --lines print the newline counts
  67. -L, --max-line-length print the length of the longest line
  68. -w, --words print the word counts
  69. --help display this help and exit
  70. --version output version information and exit"
  71.  
  72. exit 1
  73. ;;
  74. esac

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.