[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

AWK WORD COUNT SCRIPT

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
fluked
Asked:
fluked
  • 5
  • 5
1 Solution
 
sunnycoderCommented:
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
 
flukedAuthor Commented:
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
 
sunnycoderCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
flukedAuthor Commented:
#!/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
 
Alf666Commented:
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
 
flukedAuthor Commented:
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
 
sunnycoderCommented:
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
 
flukedAuthor Commented:
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
 
sunnycoderCommented:
0
 
flukedAuthor Commented:
#!/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
 
sunnycoderCommented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now