LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 06-19-2011, 11:52 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,290

Rep: Reputation: 50
Counting words in an ASCII text file.


Kernel 2.6.21.5, slackware 12.0

Hi:
I have an ASCII text file (the text of a letter I'm writing to a friend of mine, for instance) and I want to know how many words it contains. Obviously enough, some combination of linux commands will do the job. But what commands?

If I were to write the program, I would count occurrences of anything belonging to the set {space, TAB, LF}. Simpler impossible. But I do not know C (I'm enbarrased to admit).

For me the ideal scenario would be vi or vim doing the job, for I write the text using these editors (I'm switch over form vim to vi).

Last edited by stf92; 06-19-2011 at 11:55 AM.
 
Old 06-19-2011, 11:55 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I would use the 'wc' programme

Code:
wc -w filename
 
1 members found this post helpful.
Old 06-19-2011, 11:55 AM   #3
cfdisk
Member
 
Registered: May 2011
Location: Philadelphia, PA
Distribution: Slackware 13.37
Posts: 89

Rep: Reputation: 16
Try
Code:
wc --help
Beating up by Snark1994

Last edited by cfdisk; 06-19-2011 at 11:58 AM. Reason: someone is faster
 
1 members found this post helpful.
Old 06-19-2011, 12:11 PM   #4
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,290

Original Poster
Rep: Reputation: 50
Awesome!
 
Old 06-19-2011, 12:20 PM   #5
DavidMcCann
Senior Member
 
Registered: Jul 2006
Location: London
Distribution: CentOS, Salix
Posts: 3,229

Rep: Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825
If you want to check the numer of words from inside vi, use
:%!wc
followed by
u
to get your document back.
 
1 members found this post helpful.
Old 06-19-2011, 01:02 PM   #6
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by DavidMcCann View Post
If you want to check the numer of words from inside vi, use
:%!wc
followed by
u
to get your document back.
No need for that in vim. Just type g CTRL-G.
:help word-count for more info.
 
1 members found this post helpful.
Old 06-19-2011, 04:12 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,290

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by DavidMcCann View Post
If you want to check the numer of words from inside vi, use
:%!wc
followed by
u
to get your document back.
I like this solution better. It shows the inner workings of things in a closer way. What's the role of '%'? As wc needs an argument, I guess it has something to do with it. Also, this argument would be positional parameter 1 in the command line.
 
Old 06-19-2011, 04:56 PM   #8
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by stf92 View Post
I like this solution better. It shows the inner workings of things in a closer way. What's the role of '%'? As wc needs an argument, I guess it has something to do with it. Also, this argument would be positional parameter 1 in the command line.
% is a line range, it selects all lines in the active buffer for the command following it. It's a shortcut to 1,$ range (from first to last line).

So :%!wc selects all lines in the active buffer, then runs wc with shell and pipes all the lines as stdin for it. stdout is returned to replace the selected lines. This is a powerful way to use external commands within vi to manipulate opened files.

As for the argument, simply provide it like you would do in the shell prompt:
Code:
:%!wc -w
 
1 members found this post helpful.
Old 06-19-2011, 05:06 PM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,290

Original Poster
Rep: Reputation: 50
Thanks for your reply. It is very illustrative.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Prepend to ASCII text file in C MS3FGX Programming 4 06-22-2010 11:09 PM
Editing a text file (switching around words) SentralOrigin Linux - General 2 07-17-2008 04:15 PM
Replacing words in a text file Raghavan_sat Programming 3 05-27-2008 04:11 PM
counting words in file and sorting them according to their frequency mystical dervish Programming 4 05-10-2007 09:58 AM
Displaying a text file in ascii?? bauld Linux - General 4 11-21-2001 08:33 PM


All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration