Create the equivalent of the command WC


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Create the equivalent of the command WC
# 8  
Old 08-13-2013
What's wrong with wc anyway? Smilie

I suppose you could also use:-
Code:
grep -c "" file

...amongst other things, but wc is written for the purpose. Smilie



Robin
# 9  
Old 08-13-2013
Quote:
Originally Posted by rbatte1
What's wrong with wc anyway? Smilie
Perhaps the key word here is in the opening post (#1): "exercise". I doubt that there is any value in training anything if you let others do the exercising - you won't grow muscles from watching others lifting weights, so to say.

Normally i would at best ignore such a thread (or close it, if i think it is not only exercise but homework), but one thing I think i can mention without jeopardizing the intention of the exercise at all:

Quote:
Originally Posted by rbatte1
I suppose you could also use:-
Code:
grep -c "" file

...amongst other things
wc is not only counting lines (wc -l) but also words (wc) or characters (wc -c). A script intending to replace wc would have to implement all these functions, IMHO. No?

I hope this helps.

bakunin
# 10  
Old 08-13-2013
Quote:
Originally Posted by bakunin
wc is not only counting lines (wc -l) but also words (wc) or characters (wc -c).
Minor correction: -c counts bytes. Characters are counted with -m.

Regards,
Alister
# 11  
Old 08-13-2013
Quote:
Originally Posted by rbatte1
What's wrong with wc anyway? Smilie

I suppose you could also use:-
Code:
grep -c "" file

...amongst other things, but wc is written for the purpose. Smilie

Robin
Certain grep versions give RE error.
Code:
grep -c "^"

is a complete RE Smilie

---------- Post updated at 12:17 PM ---------- Previous update was at 12:08 PM ----------

The following emulates a wc without arguments:
Code:
awk '{c+=(length+1); w+=NF} END {printf " %-6s %-6s %s%s\n",NR,w+0,c+0,(FILENAME=="-")?"":" "FILENAME}'


Last edited by MadeInGermany; 08-13-2013 at 02:26 PM..
# 12  
Old 08-13-2013
Quote:
Originally Posted by MadeInGermany
The following emulates a wc without arguments:
Code:
awk '{c+=(length+1); w+=NF} END {printf " %-6s %-6s %s%s\n",NR,w+0,c+0,(FILENAME=="-")?"":" "FILENAME}'

The emulation is imperfect in at least two respects.

length counts characters while wc counts bytes.

wc implementations usually delimit words with the space ctype, which is a superset of the default blank class which awk uses to split records. This means that your awk emulation may underestimate the word count if the FS isn't set appropriately.
Code:
$ printf 'one\vtwo\vthree\n'
one
   two
      three

$ printf 'one\vtwo\vthree\n' | wc -w
3

$ printf 'one\vtwo\vthree\n' | awk '{print NF}'
1

$ printf 'one\vtwo\vthree\n' | awk '{print NF}' FS='[[:space:]]+'
3

Regards,
Alister

Last edited by alister; 08-13-2013 at 03:08 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

What is the equivalent command of sysadmsh in UNIXware7.1.3?

Sir I have read the book SCO Unix System Administrators Guide and I have found the command sysadmsh useful for system administration. I have tried the above command in unixware7.1.3 and I got the message command not found. Currently I am running unixware7.1.3 in VmWare WorkStsation10.0. ... (1 Reply)
Discussion started by: rupeshforu3
1 Replies

2. Windows & DOS: Issues & Discussions

DOS Equivalent of UNIX Command

Hi, The title of this post is a little vague but I couldn't think of what to call it. In Unix you can perform the following command ftp -v IPADDRESS <<END put FILE END In a DOS command prompt, is it possible to do the same kind of thing that the "<<END" does? So for example, ... (4 Replies)
Discussion started by: Ste_Moore01
4 Replies

3. UNIX for Dummies Questions & Answers

A faster equivalent for this sed command

Hello guys, I'm cleaning out big XML files (we're talking about 1GB at least), most of them contain words written in a non-latin alphabet. The command I'm using is so slow it's not even funny: cat $1 | sed -e :a -e 's/&lt;*&gt;//g;/&lt;/N;//ba;s/</ /g;s/>/... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. Shell Programming and Scripting

Create a .sh file for an equivalent Excel VBA code

Hi guys, I am new to Unix, Need your help here. I have installed cygwin software (Unix) in my computer (Windows vista). Now i want to create a shell script (.sh file)/other script which is equivalent of VBA code (at the bottom) and then put this .sh file into bin directory of c:/cygwin. so... (7 Replies)
Discussion started by: bansalpankaj88
7 Replies

5. UNIX for Dummies Questions & Answers

pfiles command equivalent in Linux

May i know what is the equivalent tool in linux for pflies in solaris. ? (2 Replies)
Discussion started by: mohtashims
2 Replies

6. AIX

smitty equivalent command line command

i know after you do something in smitty via the gui, you can click something in smitty that will show you how to do the same thing via the command line, (not using the gui) can anyone tell me how (2 Replies)
Discussion started by: asyed123
2 Replies

7. UNIX for Advanced & Expert Users

Need svcs equivalent command

Hi, I am new to HP-UX. Can someboby help me with the svcs equivalent command in HP-UX ??? svcs is command that we use in Solaris for service status. I need to get the status of services in HP-UX. Thanks in advance. (2 Replies)
Discussion started by: EmbedUX
2 Replies

8. Shell Programming and Scripting

Equivalent command to 'stat'

Can anyone tell me which is the equivalent command to 'stats' in ksh shell which discribes the file system? Thanks in advance Regards, Im_new (6 Replies)
Discussion started by: im_new
6 Replies

9. Solaris

Solaris equivalent of the ioscan command

What is the Solaris equivalent of the HP-UX "ioscan -funC" command which lists all hardware on the system? (5 Replies)
Discussion started by: etc
5 Replies

10. UNIX for Advanced & Expert Users

Hw to create root-equivalent accounts?

Hi all. After installing ssh on a server, i'd like to create a user with root privileges. My problem is that after creating a user rootssh (uid=0, gid=20, /home/rootshh), i make rootssh's ssh keys. The problem is that normally the ssh-keygen should create the keys under $HOME/.ssh/, and actually... (6 Replies)
Discussion started by: penguin-friend
6 Replies
Login or Register to Ask a Question