Tuesday, February 2, 2016

AWK: Calling Bash Version of echo Command with \c Option

I don't know how to embed Bourne shell echo with \c option inside AWK script, so I did this way.

echo.bash

#!/bin/bash
for i; do
echo -n $i
done


AWK script using above echo.bash script, so you can call bash version of echo:

$ awk -F, '{
cmd="./echo.bash \""$2"\" | openssl dgst -sha256"
while (cmd | getline line){
print line
}
close(cmd)
}' ./test.txt

Translation (翻訳): Rental Room Informations

The first translation:

Room: 
Fully furnished room on ground floor 
Room available now
New renovation 
Keyed entry
Large closet 
Double size bed, desk


Restriction: 
NO smoker
NO pet
NO overnight guests 

Shares:
Shared kitchen 
Shared laundry with 2 other roommates in ground suites 
Shared washroom, living room and dining room 

Includes:
Wifi, hydro, hot water

Location: 
10-15mins walking distance to Renfrew sky train station 
Central location, Superstore, Starbucks, restaurants are near by
do NOT contact me with unsolicited services or offers

お部屋:
・1階の家具付部屋
・現在空き部屋
・リフォーム済
・鍵付きエントリー
・大きなクローゼット
・ダブルベッドと机

制限事項:
・完全禁煙
・ペット不可 
・宿泊ゲスト不可

共有スペース:
・共有キッチン 
・共有ランドリー(1階のその他2名のルームメートと共有)
・共有バスルーム、共有リビングルーム、共有ダイニングルーム

設備:
・WiFi、水道、温水
 
場所:
スカイトレインの駅から歩いて10分から15分。
近くにはセンター街、スーパーマーケット、スターバックス、レストランなど有り。

※スパムメールやキャッチセールは送らないでください。

The second translation:

Room: 
Fully furnished large suite on ground floor with large window
Room available now
New renovation 
Keyed entry
Walk-in closet 
Double size bed, desk, shelf and dresser
Brand new Private bathroom with stand up shower

Restriction: 
NO smoker
NO pet
NO overnight guests 

Shares:
Shared kitchen 
Shared laundry with 2 other roommates in ground suites 
Shared living room and dining room 

Includes:
Wifi, hydro, hot water

Location: 
10-15mins walking distance to Renfrew sky train station 
Central location, Superstore, Starbucks, restaurants are near by
do NOT contact me with unsolicited services or offers

お部屋:
・リフォーム済の大きな窓がある1階の家具付部屋
・現在空き部屋
・リフォーム済
・鍵付きエントリー
・大型の衣類収納スペース、棚、鏡台
・ダブルベッドと机
・リフォーム済の私用シャワー付きバスルーム

制限事項:
・完全禁煙
・ペット不可 
・宿泊ゲスト不可

共有スペース:
・共有キッチン 
・共有ランドリー(1階のその他2名のルームメートと共有)
・共有リビングルーム、共有ダイニングルーム

設備:
・WiFi、水道、温水
 
場所:
スカイトレインの駅から歩いて10分から15分。
近くにはセンター街、スーパーマーケット、スターバックス、レストランなど有り。

※スパムメールやキャッチセールは送らないでください。

Monday, February 1, 2016

Albert Einstein (アルベルト・アインシュタイン): アインシュタインロマン 第1回 黄泉の時空から

The Difference Between Bash and Bourne Shell When Using echo Command

Bash

$ echo -n "A. P. Balachandran" | md5
b9114dc37ab93677d533a0e3e025ee58

Bourne shell: Same value!

sh-3.2$ echo "A. P. Balachandran\c" | md5
b9114dc37ab93677d533a0e3e025ee58

Bourne shell: wrong md5 value!

sh-3.2$ echo -n "A. P. Balachandran" | md5
9e63b57c592863eaad63d37f72bce0d0

Bourne shell: -n option isn't working. That is why.

sh-3.2$ echo -n "A. P. Balachandran"
-n A. P. Balachandran

AWK: Hashing Arbitrary Columns of Every Lines Using SHA-2 (SHA256) and MD5

*This code has a bug and may not work in your environment.
*This gives wrong SHA-2 or MD5 hash value.
*The echo command inside awk seems not aware of -n option.
*echo command under Bourne shell also has this behaviour.

SHA-2 (SHA256) hash every second column values in comma separated values:

awk -F, '{
cmd="echo -n \""$2"\" | openssl dgst -sha256"
while (cmd | getline line){
print line
}
close(cmd)
}' ./test.txt


MD5 (message-digest algorithm) hash every second column values in comma-separated values:

awk -F, '{
cmd="echo -n \""$2"\" | md5"
while (cmd | getline line){
print line
}
close(cmd)
}' ./test.txt

SHA-2: Command Comparison

Using command "shasum":
$ echo -n "test" | shasum -a 256
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -


Using command "openssl dgst":
$ echo -n "test" | openssl dgst -sha256
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08


Using command "openssl dgst" with option "hmac" with secret key:
$ echo -n "test" | openssl dgst -sha256 -hmac \"SECRET\"
c5790721d9fc3b84a46034c7b80d7b01d92f41a2847e67d17d8cc6cc97a6c7ad


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)