tonybaldwin | blog

non compos mentis

Posts Tagged ‘bash

Web Word Count

leave a comment »

Web Word Count: Get the word count for a list of webpages on a website.

A colleague asked what the easiest way was to get the word count for a list of pages on a website (for estimation purposes for a translation project).

This is what I came up with:

#!/bin/bash
# add up wordcounts for website

total=0 # initialize variable for total

# scan through a list of pages
# strip the html elements and count the words
# append the count to wordcount.txt

for i in $(cat pagelist.txt); do
     curl -s  $i |  sed -ne '{s/]*>//g;s/^[ \t]*//;p}' | wc -w >> wordcount.txt
done

# this is for purely aesthetic purposes, 
# but we're merging the list of pages with the wordcount file:
paste pagelist.txt wordcount.txt > pagewordcount.list

# for each number in the wordcount.txt file, add it to the previous number (get a total)
for t in $(cat wordcount.txt); do 
	total=$((total + $t))
done

# append the total to the end of the merged pagelist+wordcount file:
echo "Total word count = $total" >> pagewordcount.list

# read back the file:
cat pagewordcount.list

# ciao
exit

I ssh-ed to my server and did
ls -1 *.html > pagelist.txt
which lallowed me to feed the script this list.

baldwinlinguas.com/index.html
baldwinlinguas.com/esp.html
baldwinlinguas.com/fran.html
baldwinlinguas.com/port.html
baldwinlinguas.com/empregos.html
baldwinlinguas.com/transquote.html

So, then I ran the script on this list of the pages, and this is the output:

baldwinlinguas.com/index.html 535
baldwinlinguas.com/esp.html 342
baldwinlinguas.com/fran.html 295
baldwinlinguas.com/port.html 337
baldwinlinguas.com/empregos.html 662
baldwinlinguas.com/transquote.html 244
Total word count = 2415

So, it works. Someone with better bash fu could likely find a shorter path to this result.

Now, this is simple, of course, for a simple website, like baldwinlinguas.com.
On the other hand, if you have some huge wordpress installation, like this blog, and have tonso public php pages, rather than html, and eve more php files in the backend, you have to do a bit of sorting, I imagine.

Were I to attempt that with the baldwinsoftware wiki, I would probably just go to the Sitemap and grab that list of pages, using their URLs, of course.

./tony

Written by tonybaldwin

September 21, 2011 at 5:25 am

Web Word Count – count the words on a website with bash, lynx, curl, wget, sed, and wc

with one comment

Web Word Count: Get the word count for a list of webpages on a website.

A colleague asked what the easiest way was to get the word count for a list of pages on a website (for estimation purposes for a translation project).

This is what I came up with:

#!/bin/bash

# get word counts and generate estimated price for localization of a website
# by tony baldwin / baldwinsoftware.com
# with help from the linuxfortranslators group on yahoo!
# released according to the terms of the Gnu Publi License, v. 3 or later

# collecting necessary data:
read -p "Please enter the per word rate (only numbers, like 0.12): " rate
read -p "Enter currency (letters only, EU, USD, etc.): " cur
read -p "Enter domain (do not include http://www, just, for example, somedomain.com): " url

# if we've run this script in this dir, old files will mess us up
for i in pagelist.txt wordcount.txt plist-wcount.txt; do
	if [[ -f $i ]]; then
		echo removing old $i
		rm $i
	fi
done

echo "getting pages ...  this could take a bit ... "

wget -m -q -E -R jpg,tar,gz,png,gif,mpg,mp3,iso,wav,ogg,ogv,css,zip,djvu,js,rar,mov,3gp,tiff,mng $url
find . -type f | grep html > pagelist.txt

echo "okay, counting words...yeah...we're counting words..."

for file in $(cat pagelist.txt); do
	lynx -dump -nolist  $file | wc -w >> wordcount.txt
done
paste pagelist.txt wordcount.txt > plist-wcount.txt

echo "adding up totals...almost there..."
total=0
for t in $(cat wordcount.txt); do
	total=$((total + t))
done

echo "calculating price ... "
price=`echo "$total * $rate" | bc`

echo -e "\n-------------------------------\nTOTAL WORD COUNT = $total" >> plist-wcount.txt
echo -e "at $rate, the estimated price is $cur $price
------------------------------" >> plist-wcount.txt

echo "Okay, that should just about do it!"
echo  -------------------------------
sed 's/\.\///g' plist-wcount.txt > $url.estimate.txt
rm plist-wcount.txt
cat $url.estimate.txt
echo This information is saved in $url.estimate.txt
exit

So, then I ran the script on my site, tonybaldwin.net, with a rate of US$012/word, and this is the final output:

—————————————-
tonybaldwin.net/log/archives/environment/index.html 38
tonybaldwin.net/log/archives/cuisine/index.html 38
tonybaldwin.net/log/archives/music/index.html 52
tonybaldwin.net/log/archives/philosophy/index.html 38
tonybaldwin.net/log/archives/nanoblogger-help/index.html 52
tonybaldwin.net/log/archives/2011/09/11/911/index.html 322
tonybaldwin.net/log/archives/2011/09/index.html 774
tonybaldwin.net/log/archives/2011/09/01/mit_intro_to_cs_and_programming_assignment_1/index.html 494
tonybaldwin.net/log/archives/2011/08/26/come_on_irene/index.html 382
tonybaldwin.net/log/archives/2011/08/26/welcome_to_nanoblogger_3_4_2/index.html 289
tonybaldwin.net/log/archives/2011/08/26/here_we_roll_again/index.html 618
tonybaldwin.net/log/archives/2011/08/27/couldnt_stand_the_weather/index.html 93
tonybaldwin.net/log/archives/2011/08/index.html 1205
tonybaldwin.net/log/archives/2011/index.html 133
tonybaldwin.net/log/archives/technology/index.html 56
tonybaldwin.net/log/archives/politic/index.html 38
tonybaldwin.net/log/archives/religion/index.html 38
tonybaldwin.net/log/archives/art/index.html 38
tonybaldwin.net/log/archives/index.html 85
tonybaldwin.net/log/archives/personal/index.html 65
tonybaldwin.net/log/archives/health/index.html 38
tonybaldwin.net/log/articles/about/index.html 671
tonybaldwin.net/log/index.html 2027
tonybaldwin.net/log.1.html 2027
tonybaldwin.net/index.html 96
tonybaldwin.net/social.html 82

———————————————–
TOTAL WORD COUNT = 9789
at 0.12, the estimated price is USD 1174.68
———————————————–

Now, this is simple, of course, for a simple website, like tonybaldwin.net, which is largely all static html pages. Sites with dynamic content are going to be an entirely different story, of course.

The comments explain what’s going on here, but I explain in greater detail here on the baldwinsoftware wiki.

Now, if you just want the wordcount for one page, try this:

    #!/bin/bash

# add up wordcounts for one webpage

if [[ ! $* ]]; then
    read -p "Please enter a webpage url: " ur
else
    url=$*
 fi
 read -p "How much to you charge per word? " rate
 count=`lynx -dump -nolist $url | wc -w`
 price=`echo "$count * $rate" | bc`
 echo -e "$url has $count words. At $rate, the price would be US\$$price."
 exit

Special thanks to out to the Linux 4 Translator list for some assistance with this script.

Enjoy!

./tony

Written by tonybaldwin

September 20, 2011 at 10:31 pm

Posting to Posterous with curl

leave a comment »

Okay, so I DID successfully post with curl (thanks to some clarifications frmo John Tucker). Now, I have written a script that will allow me to write a post in vim and fire it off. It looks like this:

#!/bin/bash 
# post to posterous from bash cli with curl 

# I could do this with read -p "Enter ur username d0od: " username 
# kind of thing, but I just hardwired my info in. 
# edit accordingly for your info. 
username=YOUR_USERNAME 
password=YOUR_PASSWORD 
apitok=YOUR_API_TOKEN 

# creates a date stamp for naming the post file 
filedate=$(date %m%d%y%H%M%S) 

# set 
post title read -p "Enter a post title: " ptitle 

# write post in vim 
vim $filedate.ppost pbody="$(cat $filedate.ppost)" 

# send post to posterous with curl 

if [[ $(curl -X POST -u $username:$password -d "api_token=$apitok" \
-d "post[title]=$ptitle" -d "post[body]=$pbody" \
http://posterous.com/api/2/sites/1814977/posts | grep error) ]]; then 
echo "Too bad, do0d...FAIL!" 
else 
echo "Success! Posted to Posterous!" 
fi 

mv $(pwd)/$filedate.ppost ~/Documents/fposts/ 
# moved post to dir for safekeeping. 
# you can use different dir, d00d 

exit

This means that I have consquered the posterous API, and will now be able to add posterous support to Xpostulate.

Written by tonybaldwin

September 19, 2011 at 6:48 pm

Fren.Tcl and Frendi.Sh

with 3 comments

Friendika

Friendika

So, those who know me know that I’ve been playing on Friendika, a decentralized, federated, free/open source, privacy protecting, and, well, pretty amazing Social Networking application.

Friendika is pretty awesome in various ways, including, first, you have complete control over who can or cannot see your content.  You own your content and your privacy is completely yours to control.  Also, you can follow contacts from many other networks, including twitter, any status.net installation, Diaspora and Facebook, plus rss feeds, even, so, it becomes sort of a social networking aggregator.  Not only that, but it has friend groups similar to Diaspora Aspects or Google+ Circles.  These groups are very handy.  I follow my Diaspora and Facebook contacts, plus my identi.ca contacts, plus a large number of twitter accounts on my friendika, and have them grouped into local friends, family, haxors (fellow foss hackers, tech blogs, etc.), friends (not local, people I met online), tradus (translation colleagues, work related, polyglots), and one more group for news which includes mostly twitter feeds from a number of news outlets (Al Jazeera, BBC, NPR, Alternet, etc.).  So, it has really helped me to organize my social networking.

So, these past couple of days I, being the geek that I am, have been playing with means of posting to Friendika remotely, first from the bash cli.  Now, I had posted earlier a quick-n-dirty update type script, but I have one now that will toggle cross-posting to various other services (statusnet, twitter, facebook), and will open an editor (vim) to allow you to write longer posts.  I posted it on the wiki here, but will also include the code in this post:

#!/bin/bash

# update friendika from bash with curl
# I put this in my path as "frendi"

# here you enter your username and password
# and other relevant variables, such as whether or not
# you'd like to cross post to statusnet, twitter, or farcebork

read -p "Please enter your username: " uname
read -p "Please enter your password: " pwrd
read -p "Cross post to statusnet? (1=yes, 0=no): " snet
read -p "Cross post to twitter? (1=yes, 0=no): " twit
read -p "Cross post to Farcebork? (1=yes, 0=no): " fb
read -p "Enter the domain of your Friendika site (i.e. http://friendika.somesite.net): " url

# if you did not enter text for update, the script asks for it

if [[ $(echo $*) ]]; then
	ud="$*"
else
	read -p "Enter your update text: " ud
fi

# and this is the curl command that sends the update to the server

if [[ $(curl -u $uname:$pwrd  -d "status=$ud&statusnet_enable=$snet&twitter_enable=$twit&facebook_enable=$fb"  $url/api/statuses/update.xml | grep error) ]]; then

# what does the server say?

	echo "Error"
else
	echo "Success!"
	echo $ud
fi

# this next is optional, but I made a dir in ~/Documents to keep the posts.
# You can comment it out, you can change where it is storing them (the dir path)
# or, even, if you don't want to save the posts (they will pile up), you could
# change this to simply
# rm $filedate.fpost or rm -rf *.fpost, or some such thing.

mv $filedate.fpost ~/Documents/fposts
exit

But I have also now written a graphical application in tcl/tk to write posts to Friendika, Fren.Tcl

Fren.Tcl

Fren.Tcl - tcl/tk Friendika posting application

Find me on Friendika here.

./tony

Written by tonybaldwin

September 14, 2011 at 8:27 am

Image UP

leave a comment »

Image Up

a quick-n-dirty script to copy an image (or other file) to your server. (wiki page for this script)

I basically use this to upload screenshots for display here on this wiki and my blog, etc., so have the images directory “hardwired” in the script, but this could easily be customized to choose a different directory and use with any manner of files.

#!/bin/bash

# script to upload images to my server
# by tony baldwin

if [ ! $1 ]; then
        # If you didn't tell it which file, it asks here
	read -p "Which image, pal? " img
        else
        img = $1
fi

# using scp to copy the file to the server
scp $img username@server_url_or_IP_address:/path/to/directory/html/images/
# you will be asked for your password, of course.  This is a function of scp, so not written into the script.

echo "Your image is now at http://www.yoursite.com/images/$img."
read -p "Would you like to open it in your a browser now? (y/n): " op

if [ $op = "y" ]; then
	# you can replace xdg-open with with your favorite browser, but this should choose your default browser, anyway.
	xdg-open http://www.yoursite.com/images/$img
        # if you chose yes, the browser will open the image.
        # Otherwise, it won't, but you have the url, so you can copy/paste to a browser or html document, blog entry, tweet, etc., at will.
fi

exit

This image was uploaded with the above script:

(editing website with Tcltext)

This script, of course, assumes you are in the same directory as your image file, too.

Enjoy!

./tony


EDIT: What would be cool is if I could make your filemanager allow this in a right-click action. Like, I use PCManFM. If I could just right-click an image and choose this, then pop-up the url with zenity, or, perhaps, even just automatically run the xdg-open…Hmmmm…One can probably work this out with some filemanagers more easily than others.

With some work, I could rewrite the script so that it choose a clicked image and auto-opens with the browser, and then just choose the script with “right-click > open with …”, perhaps…

Of course, I can just F4 (open dir in terminal), then bang off the script.

Written by tonybaldwin

September 4, 2011 at 1:31 pm

Get me some learnin' (MIT Open Courseware)

leave a comment »

So, I decided,”Enough with the tinkering with the hackery…Time to start learning for real.”, and started to take this course (Introduction to Computer Science and Programming) through MIT’s Open Courseware.

So far, I haven’t even “attended” the first lecture (watch a video), but skipped ahead to the first assignment, being like that, and jumped right to solving it. The assignment was to create a program, in any language, that asks the user for their last name, then first name, then prints back the first name, then last name. I did it in 6 different languages, just for fun. 😀

First python:

#!/usr/bin/env python

# first assignment for MIT intro comp sci class.
# Problem Set 0
# Name: Tony Baldwin
# Collaborators: none
# Time: 0:30

last = raw_input("Please enter your last name:  ")
first = raw_input("Please enter your first name: ")
print("Hello, " + first + " " +  last + "!")

Then bash:

#!/bin/bash

# MIT Intro to CS & Programming, assignment 1
# by tony baldwin

read -p "Please enter your last name: " last
read -p "Please enter your first name: " first
echo Hello, $first $last!

Then lisp:

#!/usr/bin/clisp

; MIT Intro to CS & Programming, assignment 1
; tony baldwin

(format t "Please enter your last name: ")
    (let ((last (read)))
(format t "Please enter your first name: ")
     (let  ((first (read)))
     (format t "~%Hello, ~A ~A!" first last)))

Then tcl:

#!/usr/bin/env tclsh8.5

# MIT Intro to CS & Programming, assignment 1
# by tony baldwin

puts "Please enter your last name: "
gets stdin last
puts "Please enter your first name: "
gets stdin first
puts "Hello, $first $last!"

Then perl:

#!/usr/bin/perl -w
use strict;
# MIT Intro to CS & Prog
# assignment PS0

print "Please enter your last name: ";
chomp($last = );
print "Please enter your first name: ";
chomp($first = );
print "Hello, $first $last!\n";

And now, some ruby:

#!/usr/bin/ruby

# MIT Intro to CS & Programming
# assignment ps0

puts "Please enter your last name: "
last = gets
puts "Please enter your first name: "
first = gets
puts "Hello, " + first.chomp + " " +  last.chomp + "!"

I do know that eventually I will have to attend the lectures, but this first assignment seemed rather straightforward.

I was motivated to start really learning my hackery when, a couple of nights ago, I started to look at lisp.  Something in the sparse efficiency of lisp struck me as, well, striking. Beautiful, even. Weird…

./tony

Written by tonybaldwin

September 1, 2011 at 6:18 am

search wiktionary from the bash cli

with 2 comments

Last week I posted several scripts for searching google, google translate, google dictionary, reverso, and wikipedia from the bash command line.

Today I wrote another script, this time for searching wiktionary.org, the multilingual, user-edit online dictionary:

#!/bin/bash

# get definitions from wikitionary

if [ ! $1 ];
	then
read -p "Enter 2 letter language code: " lang
read -p "Enter search term: " sterm
lynx -dump $lang.wiktionary.org/wiki/$sterm | less
else
lynx -dump $1.wiktionary.org/wiki/$2 | less
fi

I tucked this into my PATH as simply “wikt”, and usage is thus:
you@machine:~$ wikt en cows
or, if you neglect to use the language code and search term, of course, the script asks for them:
you@machine:~$ wikt
Enter 2 letter language code: en
Enter search term: cows

Enjoy!

./tony

Written by tonybaldwin

May 16, 2011 at 7:11 am