-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-liners
107 lines (88 loc) · 3.88 KB
/
one-liners
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# evaluates ruby code from the command line (non-irb)
ruby -e 'puts "hello world"'
# STDIN points to the same object as $stdin; some say the constant is prettier
printf "foo\nbar\n" | ruby -e 'STDIN.each {|l|puts l.upcase}'
# The -ne flag (n with e) can be approximated as the following while loop
while gets
# do code things
end
# Cryptic globals, like $_, is the variable assigned to each processed line
# So line 5 becomes below and reminds me of arr.map{|n|&:+} or whatever
printf "foo\nbar\n" | ruby -ne 'puts $_.upcase'
#If you leave off the -n, you get a NoMethodError
#% printf "foo\nbar\n" | ruby -e 'puts $_.upcase'
#-e:1:in `<main>': undefined method `upcase' for nil:NilClass (NoMethodError)
# print takes $_ as a default argument
printf "foo\nbar\n" | ruby -ne 'print'
# => foo
# => bar
printf "foo\nbar\n" | ruby -ne 'print if $_.start_with? "f"'
# => foo
printf "foo\nbar\n" | ruby -ne 'print if /^f/'
printf "foo\nbar\n" | ruby -ne 'print if %r{^f}'
printf "foo\nbar\n" | ruby -ne 'print if %r{\Af}'
# => foo
# you can use gsub and sub with just -e, but you must use -n to operate implicitly on $_
echo 'COBOL is the best!' | ruby -ne 'print gsub("COBOL", "Ruby")'
% cat scores.txt
#Bob 40
#Alice 98
#Gillian 100
#Fred 67
cat scores.txt | ruby -ne 'print if $_.split[1].to_i > 50'
#Alice 98
#Gillian 100
#Fred 67
# use Unix/Bash things simply; supplement w/ Ruby when necessary
cat scores.txt | ruby -ne 'print if $_.split[1].to_i > 50' | ruby -ne 'puts $_.split.first'
cat scores.txt | ruby -ne 'print if $_.split[1].to_i > 50' | cut -d' ' -f1
#Alice
#Gillian
#Fred
# The -p flag (p with e) can be approximated as the following while loop
while gets
# do code things just like with -n
puts $_ # <-- and do me last "plus puts"
end
echo 'COBOL is the best!' | ruby -pe 'gsub("COBOL", "Ruby")'
# no need to call print with -n
# -p == -n with print
# ---------------------------------------------------------------------
# BEGIN behaves same/similar to ||= for setting counter variable
printf "foo\nbar\nbaz\n" | ruby -ne 'BEGIN { i=1 }; puts "#{i} #{$_}"; i += 1'
printf "foo\nbar\nbaz\n" | ruby -ne 'i ||= 1; puts "#{i} #{$_}"; i += 1'
# =>
#1 foo
#2 bar
#3 baz
# END block pretty cool too; add the numbers on each pass; but only puts total
printf "12\n76\n42" | ruby -ne 'BEGIN { n = 0 }; n += $_.to_i; END { puts n }'
# => 130
# I'm not sure if this is what the author intended.
# The longest line to the naked eye is 'bb', but this script returns 'a'
printf "a\nbb" | ruby -ne 'x ||= $_; x = $_ if $_.length > x.length; END { puts x }'
# => a
# It turns out that the length of 'a\n' is 2. You'd have to do $_.chomp for this script to return 'bb'
error_log = <<-TXT # I have no idea if this is a correct multiline heredoc
[Tue Dec 30 15:25:20 2014] [error] Directory index forbidden
[Tue Dec 30 15:26:11 2014] [notice] caught SIGTERM, shutting down
[Tue Dec 30 15:26:14 2014] [notice] Digest: generating secret ...
[Tue Dec 30 15:26:14 2014] [notice] Digest: done
[Tue Dec 30 15:26:14 2014] [notice] Apache (Unix) -- resuming normal operations
TXT
# match all the chars after [error] and puts the first match
cat error_log | ruby -ne '$_ =~ /^\[.+\] \[error\] (.*)$/ and puts $1'
# => Directory index forbidden
# demonstrate using built-in UNIX tools. uniq -c means "count occurrences"
# not sure why it returns with all the preceding white space
# also differs from book because author doesn't include error_log file
cat error_log | ruby -ne '$_ =~ /^\[.+\] \[error\] (.*)$/ and puts $1' | sort | uniq -c
# => 1 Directory index forbidden
# just another example of appending more UNIX commands; -rn (natural desc)
cat error_log \
| ruby -ne '$_ =~ /^\[.+\] \[error\] (.*)$/ and puts $1' \
| sort | uniq -c | sort -rn
# each time we iteratively get closer to our final result; first 10 lines received
cat error_log \
| ruby -ne '$_ =~ /^\[.+\] \[error\] (.*)$/ and puts $1' \
| sort | uniq -c | sort -rn | head -n 10