Solved

AWK WORD COUNT SCRIPT

Posted on 2004-04-12
13
344 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 VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cannot connect to openvpn server 9 60
ipconfig not working on windows 10 bash shell; what other commands are missing. 17 126
Linux hostname change 2 56
How to learn Linux? 10 44
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
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 …
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.
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.

863 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

26 Experts available now in Live!

Get 1:1 Help Now