Showing posts with label head (Unix). Show all posts
Showing posts with label head (Unix). Show all posts

Saturday, June 3, 2017

macOS Sierra: Generating Integers with Arbitrary Size

Command (Testing with 15 digits):

$ rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w15 | head -1 | sed 's/^0*//;'); echo $rnd; echo $(($rnd / 2));


Result (Second line is the number half the size of the first line to test if arithmetic operation works):

585206567422781
292603283711390

Wednesday, June 8, 2016

Blogger: Total Posts in This Month

Command:

$ curl -vs "http://smashingtheory.blogspot.jp" 2>&1 | grep -o -m 2 "<span class='post-count' dir='ltr'>([0-9]*)" | tail -1 | sed -e 's/[^[[:digit:]]]*//g'

Result:

33

Thursday, February 4, 2016

Get a Random Line from CSV file

First line in a CSV file contains column names, so I ignore the line and get a line after it:

$ head -$[$[${RANDOM} % $[`wc -l < ./lines.csv` - 1]] + 2] ./lines.csv | tail -1

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)