Tuesday, February 9, 2016

Homebrew: Installing Homebrew Package Manager on OS X 10.11.3

Installing a package manager Homebrew on Mac OS X 10.11.3:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Perl: iTunes XML to CSV (Without XML Parser)

itunes.pl

use constant MODE_NONE => 0;
use constant MODE_FIND_BRAKET_NAME => 1;
use constant MODE_FIND_VALUE =>2;
use constant FALSE =>0;
use constant TRUE => 1;

$mode = MODE_NONE;
$tagName = "";
$value = "";
$keyValue = "";
$openTag = FALSE;
$closeTag = FALSE;
$isTrack = FALSE;

@itunestags = ("Track ID","Name","Artist","Album Artist","Composer","Album","Grouping","Kind","Size","Total Time","Disc Number","Disc Count","Track Number","Track Count","Year","Date Modified","Date Added","Bit Rate","Sample Rate","Play Count","Play Date","Play Date UTC","Skip Count","Skip Date","Release Date","Rating","Album Rating","Album Rating Computed","Normalization","Artwork Count","Persistent ID","Track Type","Protected","Purchased","Location","File Folder Count","Library Folder Count");

print "Track ID,Name,Artist,Album Artist,Composer,Album,Year\n";

while (defined($char = getc())){

        if ($char eq "<"){
                $mode = MODE_FIND_BRAKET_NAME;
                $tagName = "";
        }elsif ($char eq ">"){
                if ($closeTag)
                {
                        if ($value eq "Tracks"){
                                $isTrack = TRUE;
                        }elsif ($value eq "Playlists"){
                                $isTrack = FALSE;
                        }elsif ($value ~~ @itunestags){
                                $keyValue = $value;
                        }else{
                                $value =~ s/"/""/g;

                                if ($value =~ /[,"]/){
                                        $value = "\"" . $value . "\"";
                                }

                                $value =~ s/&#38;/&/g;

                                if ($keyValue ~~ @itunestags){
                                        $track{$keyValue} = $value;
                                }
                        }

                        if ($isTrack && $tagName eq "dict"){
                                printf "%s,%s,%s,%s,%s,%s,%s\n", $track{'Track ID'}, $track{'Name'}, $track{'Artist'}, $track{'Album Artist'}, $track{'Composer'}, $track{'Album'},$track{'Year'};
                                $keyValue ="";
                                %track=();

                        }

                        $value = "";
                        $closeTag = FALSE;
                }else{
                        $mode = MODE_FIND_VALUE;
                        $value = "";
                }
        }else{
                if ($mode == MODE_FIND_BRAKET_NAME){
                        if ($char eq "/"){
                                $closeTag = TRUE;
                        }else{
                                $tagName .= $char;
                        }
                }elsif($mode == MODE_FIND_VALUE){
                        $value .= $char;
                }
        }
}

Bash command:

$ cat ./itunes.xml | perl ./itunes.pl

AWK: Printing CSV file

printing CSV file:

csv.awk:

{
        readcsv($0);
        print $2" by "$3;
}

function readcsv(string)
{
        #フィールド番号
        n=1;

        #コンマ区切り
        m=split(string, array , ",");

        #完全フィールドか否か
        aflag=1;

        for(i=1;i<=m;i++){
                # "  "で括られていない完全フィールド。
                if( !(array[i] ~ /^\"/) && !(array[i] ~/\"$/) && aflag){
                        #ダブルクオートのエスケープ文字を削除
                        gsub(/\"\"/,"\"", array[i]);

                        $n=array[i];
                        n++;
                        continue;
                }

                # "  "で括られている完全フィールド
                if(( array[i] ~ /^\".*\"$/ ) && aflag){
                        #先頭と末端にあるダブルクオートを削除する
                        gsub(/^\"/,"", array[i]);
                        gsub(/\"$/,"", array[i]);
                        #ダブルクオートのエスケープ文字を削除
                        gsub(/\"\"/,"\"", array[i]);

                        $n=array[i];
                        n++;
                        continue;
                }

                # 先頭が "の部分フィールド
                if(array [i] ~ /^\"/){
                        #先頭のダブルクオートを削除
                        gsub(/^\"/,"", array[i]);
                        #ダブルクオートのエスケープ文字を削除
                        gsub(/\"\"/,"\"", array[i]);

                        aflag=0;
                        $n=array[i];
                        continue;
                }

                # "  "で括られていない、続きの部分フィールド。
                if(!(array[i] ~ /^\"/) && !(array[i] ~/\"$/) && aflag==0){
                        #ダブルクオートのエスケープ文字を削除
                        gsub(/\"\"/,"\"", array[i]);

                        $n = $n "," array[i];
                        continue;
                }

                # 末端が "の部分フィールド
                if((array [i] ~ /\"$/) && aflag==0){
                        #末端のダブルクオートを削除
                        gsub(/\"$/,"", array[i]);
                        #ダブルクオートのエスケープ文字を削除
                        gsub(/\"\"/,"\"", array[i]);

                        $n = $n "," array[i];
                        aflag=1;
                        n++;
                        continue;
                }
        }
}

Bash command:

$ cat list.csv | awk -f ./csv.awk

AWK: FS, RS, OFS, ORS

Testing FS (field separator), RS (record separator), OFS (output field separator), ORS (output record separator):

Command:

$ echo -n '"test,test",testtest,test,test' | awk 'BEGIN{FS=",";RS="\n";OFS="-";ORS="+"}{print $1, $2, $3, $4, $5, $6}'


Output:

"test-test"-testtest-test-test-+

AWK: split Function

Testing the behavior of split function in awk:

$ echo -n 'test,test,test' | awk '{m=split($0,array,",")}{print m}'
3

Saturday, February 6, 2016

Perl: Read a Character from Standard Input Until The End-of-file

Read a character from standard input until the end-of-file:

while (defined($char = getc())){
print $char;
}

Friday, February 5, 2016

Python: Print Hello

$ python -c 'print "hello"'

Version of PHP

$ php --version
PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

Version of Perl

$ perl --version

This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)

Copyright 1987-2013, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Version of AWK

$ awk --version
awk version 20070501

Version of Ruby

$ ruby --version
ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]

Version of Python

$ python --version
Python 2.7.10

Thursday, February 4, 2016

AWK: Number of Words

Printing the number of words in lines without newline:

$ awk 'BEGIN {words=0}{words+=NF} END { printf "%i", words} ./lines.txt