[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

AWK WORD COUNT SCRIPT

Posted on 2004-04-12
13
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

656 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