Solved

AWK WORD COUNT SCRIPT

Posted on 2004-04-12
13
339 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
 
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now