Denny’s OpenBSD Newbies blog

April 30, 2010

More On Text Processing

Filed under: openbsd — Tags: , , , , , , , — denny @ 6:57 pm

I kept fooling around with a text file the other night, trying to get an
exact character count. The file had 11 lines and I came up with 436
characters, counting them manually. That included whitespace between
the letters and numbers. I ran wc(1) on it to see what it would say:

$ wc -m file
          447

That was a difference of 11 more than what I had counted. How come?
Turns out it counts line feeds as characters, too. Okay, how can I get
rid of them to get the same count as I had gotten counting manually?
I can use sed(1) to do it:

$ sed -e ’s/.$//’ file |wc -m
            436

Actually, I can do the same without the -e switch on it. It got thrown in
the mix and then I realized I didn’t need it. We usually don’t think of a
whitespace as a character, but it is. To see how many characters there
are minus whitespace, I can use tr(1):

$ tr -d ‘[:blank:]‘ < file | wc -c
         373

I know the total number of characters including whitespace and line feeds
is 447. Okay, bc(1) to the rescue:

$ echo “447-373″ | bc
         74

If I use space instead of blank it takes out the line feeds too:

$ tr -d ‘[:space:]‘ < file | wc -c
         362

You add the original 11 line feeds to that and you’re back at 373.

You can also count the characters minus the line feeds and whitespaces
while editing the file in vim like so:

%s/\S/&/gn

So, the adventures in text processing continue. :-) Any input on these
methods and any additional ones always welcome and appreciated.

Cheers!

June 13, 2008

Another Conky Update

Filed under: openbsd — Tags: , , , , , , , , , — denny @ 10:13 pm

I added another section at the bottom of the original page on Conky explaining how
I sort my weather forecast emails on one box, cut out the title of the latest message
and use it output the message text, format it for conky, and then send it on to the
other box where I’m using conky on the desktop. Where cut and sed format the text,
you’ll have to experiment with your particular message names and the amount of
characters you cut. Here’s what my final output looks like:

current weather and forecast displayed in Conky

Powered by WordPress

Rss Feed Tweeter button Facebook button Technorati button Reddit button Myspace button Linkedin button Webonews button Delicious button Digg button Stumbleupon button Newsvine button Youtube button