Solved

AWK WORD COUNT SCRIPT

Posted on 2004-04-12
13
356 Views
Last Modified: 2010-04-20
I'm trying to make an awk word count script that implements the following expample...
if no input is given then "usage is awc file..."
else it counts the number of words in the file(s) and prints out the total the fields are line count, word count, character count, and total.

$ ./awc
Usage is "awc file..."
$ cat file1
This is one line
This is another
$ ./awc file1
      2      7       31 file1
$ cat file2
This is one line
This is another
This is the third line
$ ./awc file1 file2
      2      7       33 file1
      3     12       56 file2
      5     19       89 total
$ _

Thanks fluked.
0
Comment
Question by:fluked
  • 5
  • 5
13 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10804505
Hi fluked,

you can use wc command ... It does exactly that

man wc

If you still insist on writing your own script, then post what you have so far

Sunnycoder
0
 
LVL 1

Author Comment

by:fluked
ID: 10804523
I need to know how this is done I haven't started so far I know wc has three swtiches

wc [-lwc] filename

I posted the question maybe someone has the solution to this problem thank you.

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10804535
fluked,

Here is the man page

NAME
       wc - print the number of bytes, words, and lines in files

SYNOPSIS
       wc [OPTION]... [FILE]...

DESCRIPTION
       Print  newline, word, and byte counts for each FILE, and a
       total line if more than one FILE is  specified.   With  no
       FILE, or when FILE is -, read standard input.

       -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

sample run

wc test1.txt test2.txt
      6      20     314 test1.txt
      7      80     438 test2.txt
     13     100     752 total

Sunnycoder
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:fluked
ID: 10804557
#!/bin/bash

if [ $# -lt 1 ]
then
 echo "Usage is \"'base $0' file...'"
else
 while [ $0 -gt 0 ]
wc -lwc $1
endwhile

i have to turn that into an awk script any suggestions?
0
 
LVL 9

Expert Comment

by:Alf666
ID: 10804639
Simply use "wc -w file"

EE has a policy against posting homework. This looks a bit like it.

Is there a particular reason you absolutely want an awk script ?
0
 
LVL 1

Author Comment

by:fluked
ID: 10804647
This isn't homework, the reason for homework is to learn, I did my homework long time ago..
This is for my job that's all I just needed help.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10804661
fluked,

You already have a native command that does the job and you have already embedded it into a script for usage message ... Even I was wondering the reason for fixation on awk

Sunnycoder
0
 
LVL 1

Author Comment

by:fluked
ID: 10804667
That's what my boss needs, believe me if i can use the sample script above then this would save me time. This is frustrating.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10804703
0
 
LVL 1

Author Comment

by:fluked
ID: 10804714
#!/bin/bash

function awc {
wc -lwc $1
}

if [ $# -lt 1 ]
then
  echo "Usage is \"'basename $0' file...\""
fi

else
 while [ $# -gt 0 ]
  do
   awc
 done

-- how does this look
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 10804728
fluked,

If you wish to use the kind of script above, you can try something like

if ( ARGC < 1 )
      print usage message
else
{
      e = sprintf("wc %s %s %s", ... your ARGV[1], etc go here ... you need a loop until ARGC to do this
      system (e );
}

In the script you posted, the while loop looks like infinite loop to me

Sunnycoder
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question