Categories
PHP Quick Post

AWK one-liners and Vim

Just a quick one – I had to edit an old PHP script that I wrote a while back to create calendar events. At the start of this file is a large array containing a number of letters which correspond to specific durations. As usual – I stuck with my one of the well known XKCD comics and decided that to update this file it was easier to learn a few new tricks (teach a man to fish and all that). In summary, I had to add to the file a list of formatted letters like

"N", 
"N",
"N",
"L",
"L"

Each letter was repeated 3 times on recurring lines, and there was a total of 189 letters which had to be wrapped in quotation marks and have a comma at the end.

To begin with, I created a text file and entered in each line one character that would eventually become 3 lines. I then wrapped this up in AWK using the following one liner;

awk '{ print "\"" $0 "\","; print "\"" $0 "\","; print "\"" $0 "\","}' input.txt > output.txt 

I know that docent look pretty, however its a one liner and it works. To start with,

print $0

will print the current line, so by prepending and appending the characters I needed, properly escaped, around that

$0

and repeating that 3 times I turned

N
L
R

into

"N",
"N",
"N",
"L",
"L",
"L",
"R",
"R",
"R",

for a total of 189 lines. I then moved this file to where I run my PHP script, and started VIM opening the two files at once. A quick press of

v 

to select Visual Mode where I select the whole file, followed by a

:b 1

to move to the PHP script (the b stands for buffer, and the 1 is because that was the first file opened. 2 would be the seconds etc.) in VIM and a quick paste and I was almost done – I just had to remove the final comma

,

from the last array entry. I’m not good enough to have a bit of AWK work the last bit out – and the time spent implementing it would not be worth it.

AWK one liners, and AWK itself has never really been something I’ve used. In the past its been presented to me as a way to solve problems, but the solutions have been handed to me. This is the first time I’ve actively went hunting for a solution without asking for help – and even though its simple, I’m quite happy I managed to figure it out – even if it did take about 20 minutes of searching…

As a final note I’m also quite happy I managed this completely in the command line. No mice were used in the editing of this script!

Categories
Electronics General PHP

A new iPhone?

Well. It’s sort of guaranteed that I’ll be buying the new iPhone when its launched. The announcement is in 4 days, and the Keynote will be streamed live. I hope it’s streamed via the Apple TV again.

Countdown to Apple Event
Countdown to Apple Event

I’m not going to post about the handset or the specifications. That has been done again and again.

My biggest issue when it’s time for me to upgrade my handset is trying to figure out the best plan, and whether its worth purchasing the handset outright and taking a SIM only deal, or taking out an actual contract.

In the UK, we have a number of options for phones. The first (and most common) option is to take out a (now standard) 24 month contract. You pay more per month, but in that monthly payment you pay for the handset and for the service. Taking an iPhone 5S for example, I can grab a 16Gb iPhone 5S on the o2 network with Unlimited Minutes and Texts with 2Gb of 4G data for £43 per month, with no charge for the handset. Thats it. You walk in, get a phone, no payment, and walk out. Alternatively, I can pay £49.99 for the handset with the above options, and pay £38 per month. So, two options already and thats for the same contract. Doing the math and basing it on the average monthly cost, ( (£38 x 24) + £49.99 = £40.08 vs £42) shows the better deal to be £40.08.

Then we have SIM only deals. This is where you purchase the device out right (usually at Apple prices, then its “Factory Unlocked” – gives a greater re-sale value) and then pay monthly just for the plan. Again, you get different deals if you take out a “rolling” 30 day contract, or a 12 month contract. Again, using o2 as an example, the same plan as above would cost £22 per month for a 30 day rolling contract, vs £20 per month for a 12 month contract. Again, doing the math and basing it on the average monthly cost over 2 years, factoring in the price of an iPhone 5S 16Gb ( (£20 x 24) + £549.00 = £42.87) shows the better deal to be £40.08, i.e paying £49.99 for the handset and paying £38 per month. I’ve only included the £20 per month tariff as I’m basing this on a 24 month contract.

So, I have maths. Thats only comparing 1 plan with 3 options. To do this with every tariff available in the UK with the various handsets would take a fair bit of time. To that end, I created a website. WhatPlanShouldIGet.uk. Its pretty simple. Right now, it only allows you to enter a purchase price for a handset. As soon as the new mobile tariffs are announced for the iPhone 6 (or whatever it will be called), I will update the site with the various bits of data required. You will then be able to choose the handset, and the site will give you a nice, already worked-out table for your comparing pleasure!

The above assumes a lot of things. It assumes you have the cash available to purchase an iPhone outright. It does not take into account any interest rates imposed by credit card companies if you purchase the handset on a credit card (although that is a pretty good idea – new feature request!) and it does not take into account any loyalty or other discounts you may be offered.

It’s just a simple, quick and easy tool to work out What Plan Should I Get.

The site itself is written in PHP. It uses MySQL for backend storage, and bootstrap to make it look nice. I’ll post the code at some point, but as its a public facing site, I want to make sure I haven’t left any data destroying bugs (either server or client side) in the code. For more information thought, you can head over to WhatPlanShouldIGet.uk

Categories
Electronics GitHub PHP

Using the terminal & PHP to save Google Reader Starred items!

*MAJOR UPDATE* – USING PHP ONLY WITH NEW CODE! Please view this next post for more information!

So. Google Reader is closing down. I’m not going to get all high and mighty – It’s Google’s product. They do with it as they wish! There are several places that let you save your feeds from Google Reader, but I wanted to add all my Starred Items from Reader into Pocket. It turns out it wasn’t the easiest thing to do! After a few terminal commands and some PHP, this is what I came up with!

Firstly, head to Google Reader, and use the Data Takeout feature that Google provides to save your Reader Data only. The outputted ZIP file should contain a folder entitled “Reader”. Within that, there should be a file named “starred.json”.

*UPDATE* – I have saved these 2 files to GitHub! Go, Grab!
https://github.com/nickwebcouk/pocketimport

Now the fun begins! To make it easy (and quick) I used the Terminal on Mac and ran these commands within the above folder. I used two files (new.txt and newnew.txt) just to keep track of what was happening. There are easier ways of doing this! I ran the following commands from terminal:

grep -a1 "canonical" starred.json > new.txt
grep -v "^\--" new.txt > newnew.txt
grep -v "} ]," newnew.txt > new.txt
grep -v "\"canonical\" : \[ {" new.txt > newnew.txt
grep -v "\"updated" newnew.txt > new.txt
grep -v "} \]," new.txt > newnew.txt
cat newnew.txt | rev | cut -c 2- | rev > new.txt
cut -c 17- new.txt > newnew.txt
rm -r new.txt
mv newnew.txt url.txt

The “grep” command looks through text files for specific expressions. “cat” outputs a full file, “rev” reverses items, “cut” cuts text, “rm” removes files and “mv” moves files.

To make this easier, I created a Shell Script (Tested on OSX 10.8.2 only)

#!/bin/sh

clear
grep -a1 "canonical" starred.json > new.txt
grep -v "^\--" new.txt > newnew.txt
grep -v "} ]," newnew.txt > new.txt
grep -v "\"canonical\" : \[ {" new.txt > newnew.txt
grep -v "\"updated" newnew.txt > new.txt
grep -v "} \]," new.txt > newnew.txt
cat newnew.txt | rev | cut -c 2- | rev > new.txt
cut -c 17- new.txt > newnew.txt
rm -r new.txt
mv newnew.txt url.txt

You can run that file by saving it to the same location as the Google Reader folder, and running the following in terminal first:

chmod 755 script.sh
./script.sh

the first line tells the computer to allow script.sh to be executed, and the second line executes the script.

I then moved the url.txt file that had just been created to my www root folder (for me its under /users/~name/sites/), and created the following PHP/HTML code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Instapaper: Export</title>
</head>
<body>
<h1>Unread</h1>
<ol>
<?php
    $file_handle = fopen("url.txt", "r");
    while (!feof($file_handle)) {
        $galbool = FALSE;
        $line = fgets($file_handle);
        $string = $line;
        $check = $string[strlen($string)-2];
        if ( $check == "/"){
            $string = rtrim($string);
            $string = rtrim($string, "/");
            $desc = $string;
        }
        $gallerycheck = str_replace("/gallery", "", $string, $count);
        if ($count == 1){
            $galbool = TRUE;
            $string = rtrim($string);
            $string = rtrim($string, "/gallery");
            $desc = $string;
        }
        $desc = strrchr($string, "/");
        $desc = str_replace("/", "", $desc);
        $desc = str_replace("-", " ", $desc);
        $desc = ucwords($desc);
        if ($galbool == TRUE){
        $formatted = '			<li><a href="' . $string . '">' . $desc . '[Gallery]</a></li>';
        } else {
        $formatted = '			<li><a href="' . $string . '">' . $desc . '</a></li>';
        }
        echo $formatted;
    }
    fclose($file_handle);
?>
</ol>
</body>
</html>

This provided me with a HTML page, which if saved as instapaper-export.html allowed me to head to getpocket.com and use the Instapaper import option.

584 starred articles and 2 seconds later, I received this wonderful little message!

getpocket import