I’ve been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because I don’t know how to easily check line length for a long series of lines. I’m on a Mac and can get around in Terminal. Is there some script you can share with me to check this, Dave?
You’ve come to the right blogger for this question, as I’ve been working on Unix, Linux and Mac OS X shell scripts and command line utilities for longer than the Internet’s been around (really, I’ve been online 31 years!) and even wrote a book about shell script programming called Wicked Cool Shell Scripts. It’s perfect!
There are two parts to this challenge, however. The first is to get all your proposed tweets into a file that’s Terminal friendly. The easiest way is to copy the set out from whatever program you’re using (Word, Pages, an email program, your Web browser) and paste them into a blank text file that you’ve created with vi or emacs. I’ll use “vi” for this: type “vi tweets” into the Terminal on your Mac, then press the “i” key to move into insert mode. Now choose Paste from the Edit menu and all the tweets you’ve written will appear. Press esc on the keyboard, then “ZZ” to write and quit.
Great. That file’s created. Next up, here’s a basic script that’ll show you the length of each tweet you’ve written, followed by the first 20 characters or so:
while read tweet
do
length=”$(echo $tweet | wc -c)”
echo $length — $(echo $tweet | cut -c1-20)…
done < tweets.txt
exit 0
The results are useful, but it’s a lot of data if you have a big campaign:
105 — #DENVER – 9th Annual Integrate…
61 — @ColoradoMom how is it we’ve n…
24 — @MargaretSchaut Thanks!…
49 — @SheilaBeal “breaking” (ouch!)…
129 — @climagic I am a big awk fan i…
139 — @delwilliams I am totally an a…
118 — @doylealbee If we’re going to …
63 — @n_quinlan send me your email …
99 — @n_quinlan wanna write a guest…
123 — @neiltaftcares glad to know I …
131 — @thebescene Looking forward to…
122 — Considering heading to the the…
90 — Early registration for my Boul…
140 — Email from Amazon, my Kindle F…
126 — Hey, look at that! Early regis…
120 — Join @MomCentral and @StacyDeB…
127 — Just submitted my review of “I…
60 — Movie Review: ‘Immortals’ – su…
Since a tweet can be up to 144 characters in length, it’d be nice to have it at least let slip all the tweets that are less than 130 characters: they’re going to work fine. So let’s do that by adding a simple conditional:
while read tweet
do
length=”$(echo $tweet | wc -c)”
if [ $length -ge 130 ]; then
echo $length — $(echo $tweet | cut -c1-20)…
fi
done < tweets.txt
exit 0
The result is a lot easier to understand because it’s only flagging those that are getting too close to the Twitter threshold:
139 — @delwilliams I am totally an a…
131 — @thebescene Looking forward to…
140 — Email from Amazon, my Kindle F…
From this point, you can hack and tweak further to get what you’d like, including checking all the tweets for spelling mistakes by tapping into the command-line spell check utility too. Most importantly, if you’re going to disseminate these tweets for others to share, you won’t have to worry about “it’s too long!” complaints. 🙂
With bash, there is no need for ‘wc -c’ or ‘cut ….’
length=${#variable}
echo ${tweet:0:20}
It works with /bin/sh as well
# /bin/sh -c “(tweet=’the quick brown fox jumps over lazy dogs…’ size_tweet=${#tweet};echo ${size_tweet};echo ${tweet:0:20})”
43
the quick brown fox
It’s also a good idea when using tweets for promotion to leave some “headroom” for the ‘RT’ and the username. If you do it properly, you won’t cut off the end of the tweet.