Sunday, January 31, 2016

Duration of execution: Comparison Between Perl & C

Perl script counting from 1 to 1,000,000,000:
$ time ./countlargenumber.pl
500000000500000000
real 0m43.157s
user 0m43.013s
sys 0m0.019s


Executable written in C counting from 1 to 1,000,000,000:
$ time ./countlargenumber.out
500000000500000000
real 0m2.057s
user 0m2.028s
sys 0m0.002s

AWK: Extracting the Values of Specified Columns Separated by a Delimiter

Extracting the second column values from lines with delimiter '|' separating each columns:

awk -F'|' '{print $2}' ./lines.txt

Women's rights: Free the Nipple?

Gender equality



  • Gender equality (男女同権)
  • Women's rights
  • Social equality

MD5: Testing Out MD5 Behaviour

MD5 hash using s option:
md5 -s "test string"
MD5 ("test string") = 6f8db599de986fab7a21625b7916589c


MD5 hash using pipe:
echo -n test string | md5
6f8db599de986fab7a21625b7916589c


MD5 hash using pipe, but echo without n option:
echo test string | md5
f299060e0383392ebeac64b714eca7e3

Sed: Replacing Delimiter Characters to Create Comma-Separated Values

Replacing all vertical lines '|' with comma ',':

cat ./lines.txt | sed s/\|/,/g

Saturday, January 30, 2016

AWK: Appending Characters to Each Line

Appending '|' character to the end of each lines:

awk '{print $0"|"}' ./input.txt > ./output.txt

Debugging: Cron: % sign

Error message:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `]'
/bin/sh: -c: line 1: syntax error: unexpected end of file


The source code:

*/10 * * * * perl -le 'sleep rand 60' && head -$[${RANDOM} % `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/tweet.rb

Crontab man page:

The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Fixed source code:

*/10 * * * * perl -le 'sleep rand 60' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/tweet.rb

Friday, January 29, 2016

Cron: Tweet a Line Every 10-19 Minutes

Cron - Tweet a line every 30-40 mins:

*/10 * * * * perl -le 'sleep rand 540' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/lines.rb

lines.rb - using standard input:

require 'json'
require 'oauth'

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = STDIN.gets

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)

Getting Random Line Number -1

Bash line to generate random line number from 1 to max - 1.
echo $(ruby -e 'print rand') $(wc -l < "./lines.txt") | awk '{printf("%d\n", $1*$2)}'

African American Inventors

Henry E. Baker
Benjamin Banneker
James Forten
Ben Montgomery
Norbert Rillieux
Jan Ernst Matzeliger
Elijah McCoy
George Franklin Grant
Lewis Howard Latimer
Granville Woods
Garrett Morgan
George Washington Carver
Madam C. J. Walker
Sarah E. Goode
Sarah Boone
Alice H. Parker
Patricia Bath

Thursday, January 28, 2016

Ruby: Tweeting Using OAuth gem

Package needed:

gem install oauth

Ruby script: tweet.rb

require 'json'
require 'oauth'

consumer_key = '' # Your consumer key
consumer_secret = '' # Your consumer secret
access_token = '' # Your access token
access_token_secret = '' # Your access token secret

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = ARGV[0]

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)



Execute the ruby script:

ruby ./tweet.rb "Good morning! (おはようございます。)"

Generating Random Password

Generating 32-character random password

$ cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1

This is the same:

$ cat /dev/urandom | env LC_CTYPE=C tr -dc '[:alnum:]' | fold -w 32 | head -n 1

Generating Random 15-Character User Name

Generating random 15-character:
$ cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | fold -w 15 | head -n 1
This is the same:
$ $ cat /dev/urandom | env LC_CTYPE=C tr -cd '[:lower:][:digit:]' | fold -w 15 | head -n 1

Walter Payton

Walter Payton