char count shell script

Submit 100 character stories for the weekly contest
User avatar
eric_marsh
Member
Posts: 993
Joined: Tue Sep 15, 2009 5:03 pm
Location: Lockhart, TX

char count shell script

Postby eric_marsh » Thu Nov 19, 2009 2:56 pm

Here's a quick little shell script I worked up to count characters less spaces.

#!/bin/sh
sed 's/[ ]*//g' $1 | tr -d '\n' | wc -c
Life is a tragedy for those who feel, but a comedy to those who think. - Horace Walpole
Eric's Drabbles: http://eric-marsh.blogspot.com/"

User avatar
strawman
Member
Posts: 5966
Joined: Sat Oct 20, 2007 2:20 pm
Location: South Georgia

Re: char count shell script

Postby strawman » Thu Nov 19, 2009 3:49 pm

Boy, do I feel dumb. In fact, this calls for a hijacked thread contest:

I feel as dumb as a _____________.
Never judge anyone until you have biopsied their brain.

"Be kind, for everyone is fighting a hard battle."
Known Some Call Is Air Am
Spoiler:
Non sum qualis eram = "I am not who I will be"

User avatar
eric_marsh
Member
Posts: 993
Joined: Tue Sep 15, 2009 5:03 pm
Location: Lockhart, TX

Re: char count shell script

Postby eric_marsh » Thu Nov 19, 2009 4:19 pm

Think of it as an arcane but powerful magic spell.

I'll conjure an XP universe and test this spell so that I can provide a more complete text of how it's used.

In the world of windows at least one doesn't have to fear daemons, but there are terrible viruses that can steal one's very identity and far more terrifying things at it's core.

I think I feel a Drabble coming on.
Life is a tragedy for those who feel, but a comedy to those who think. - Horace Walpole
Eric's Drabbles: http://eric-marsh.blogspot.com/"

User avatar
ROU Killing Time
Notorious Forum Hog
Posts: 4236
Joined: Sun Feb 01, 2009 10:07 pm
Location: Portland, OR

Re: char count shell script

Postby ROU Killing Time » Thu Nov 19, 2009 7:07 pm

If I had VFP on this computer I could do it easily.

*** Function Twabble counter
*** g_twabble
*** parameters: pc_twabble (twabble to be checked for character count
***
*** Example: ? G_TWABBLE("I'm trying to write a twabble and I need to make sure it's 100 characters so ASID doesn't get on my case, did I get it right?")
FUNCTION g_twabble
PARAMETERS pc_twabble
IF type("pc_twabble") <> "C"
RETURN "Uh, I thought you wanted me to count a twabble?"
ENDIF
pc_twabble = STRTRAN(pc_twabble," ","")
ln_count = alltrim(len(pc_twabble))
RETURN "This twabble is exactly " + alltrim(STR(ln_count,15,0)) + " characters long"
"Never fuck with The Culture"
Sublime In Peace Iain M. Banks.

User avatar
strawman
Member
Posts: 5966
Joined: Sat Oct 20, 2007 2:20 pm
Location: South Georgia

Re: char count shell script

Postby strawman » Thu Nov 19, 2009 7:39 pm

See, the way you explain it, I don't feel as dumb..
Never judge anyone until you have biopsied their brain.

"Be kind, for everyone is fighting a hard battle."
Known Some Call Is Air Am
Spoiler:
Non sum qualis eram = "I am not who I will be"

User avatar
eric_marsh
Member
Posts: 993
Joined: Tue Sep 15, 2009 5:03 pm
Location: Lockhart, TX

Re: char count shell script

Postby eric_marsh » Thu Nov 19, 2009 10:06 pm

I got a windows version working.

First you need to install cygwin from http://www.cygwin.com/. This is a free set of unix tools ported to windows. Next you need to create a file called chrcnt.bat in your working directory or a directory in your PATH. It should have this content:

@sed 's/[ ]*//g' %1 | tr -d '\r' | tr -d '\n' | wc -c

Then you just need to run the command:

chrcnt <twit text file name>

for example

chrcnt mytwit.txt

Oh, I'm sorry. chrcnt would be a unix name. On windows charactercounter would be more appropriate. Don't want to make things too easy.

There may be more elegant apps out there to accomplish the same thing but it's still much easier than counting characters by hand.
Life is a tragedy for those who feel, but a comedy to those who think. - Horace Walpole
Eric's Drabbles: http://eric-marsh.blogspot.com/"

User avatar
Algernon Sydney is Dead
Moderator
Posts: 3499
Joined: Thu Oct 16, 2008 11:22 pm
Location: PRK (California)

Re: char count shell script

Postby Algernon Sydney is Dead » Fri Nov 20, 2009 1:40 pm

I went ahead and made a web-page that checks length on the fly. All you need is a browser, no stinking command line or editor macros.

See this post.

User avatar
eric_marsh
Member
Posts: 993
Joined: Tue Sep 15, 2009 5:03 pm
Location: Lockhart, TX

Re: char count shell script

Postby eric_marsh » Fri Nov 20, 2009 2:04 pm

Very cool, and much more elegant than a shell script.
Life is a tragedy for those who feel, but a comedy to those who think. - Horace Walpole
Eric's Drabbles: http://eric-marsh.blogspot.com/"

User avatar
Console Jockey
Member
Posts: 20
Joined: Tue Feb 02, 2010 7:47 pm

Re: char count shell script

Postby Console Jockey » Wed Feb 10, 2010 10:49 pm

As is so often the case, there are many ways of achieving the same results. I like to minimize the number of commands strung together. Since tr will accept a set of characters as the parameter, I include both the space and the newline in a single tr, thereby eliminating the need for sed or multiple tr calls. The shell command I use to validate the number of characters for my twitfic is as follows:

1. If I don't want to bother with saving the twitfic to a file, I will do the following:
$ echo "blah blah blah blah this is my twitfic blah blah blah blah"|tr -d " \n"|wc -m
47


2. If on the other hand I have saved my twitfic to a simple text file called mytwitfic.txt, then I will do the following:
$ cat mytwitfic.txt|tr -d " \n"|wc -m
47


3. If the file was written and saved on a windows machine and moved to linux, to get an accurate character count you would want to translate the extraneous carriage return (\r) in addition to the newline. This can be done by simply adding \r to the tr statement as:
$ cat mytwitfic.txt|tr -d " \n\r"|wc -m
47

Note the use of the -m option for wc as opposed to -c. This will ensure that wc returns the number of characters as opposed to the number of bytes. If the twitfic was written in a unicode language that requires two bytes per character, the -c option would multiply the character count by x2.

Of course, this can be wrapped up in a nice, neat bash script which accepts both a string or a file. Also, I've included the stripping of the windows carriage return in the script as well:

Code: Select all

#!/bin/bash
## Script used to count the number of characters in either a string
## or a file, excluding spaces, new lines or carriage returns.
##
## Strings MUST be enclosed within double quotes (") or the count
## will only return the lenth of the first work in the string.
##
## Results will output both the string or file contents and the count
## of characters.
## 02/10/10 mkz (0.0.1)


OFS=$IFS;
IFS="";
show_help() {
   printf 'Count the number of characters for a twitfic\n' >&1
   printf 'Usage: %s [-s "string"] [-f file]\n' >&1
   printf ' -s\tString to count characters (minus spaces)\n' >&1
   printf ' -f\tFile to count characters (minus spaces)\n' >&1
}

while getopts ':s:f:' OPTIONS; do
   case $OPTIONS in
   s)   COUNT=$(echo "$OPTARG"|tr -d " \n\r"|wc -m);
      echo Twitfic: "$OPTARG";
      printf 'Character count: %s\n' $COUNT >&1
      IFS=$OFS;
      exit;
   ;;
   f)   if [ -r ${OPTARG} ]; then
         COUNT=$(cat "$OPTARG"|tr -d " \n\r"|wc -m);
          printf 'Twitfic: ' >&1
          cat "$OPTARG";
         printf 'Character count: %s\n' $COUNT >&1;
         IFS=$OFS
         exit;
      else
         printf 'ERROR: File [%s] missing or insufficient rights to read!\n' ${OPTARG} >&2
      fi
   ;;
   *)    show_help;
      IFS=$OFS
      exit 2;
   ;;
   esac;
done;
show_help;
IFS=$OFS;

NOTE: Could be made much more efficient, but what do you expect for 5 minutes?
-Console Jockey
kernel, n.:
A part of an operating system that preserves the medieval
traditions of sorcery and black art.

User avatar
Console Jockey
Member
Posts: 20
Joined: Tue Feb 02, 2010 7:47 pm

Re: char count shell script

Postby Console Jockey » Thu Feb 11, 2010 3:42 am

It appears that my shell script to not handle embedded double quote (") nor exclamation (!) characters correctly. Looks like I might have to invoke sed afterall...
-Console Jockey
kernel, n.:
A part of an operating system that preserves the medieval
traditions of sorcery and black art.


Return to “Twit-Fic!”



Who is online

Users browsing this forum: No registered users and 1 guest