2 ms·
Using Ruby instead of sed and awk
- Moyamo 12y agoEven though, the ruby one-liner seems more legible, it is a lot longer (and probably a lot slower). In this case I don't see what's wrong with the sed, awk or grep commands (fast and short). To explain how they work sed -i '/^$/d' input.txt # for every line matching '^$' (empty line) apply 'd' (delete the line). grep -v '^$' # print every line not matching '^$' (empty line) awk 'NF' input.txt # 'NF' stand for Number of Fields (this is equivalent to number of columns # in a table). If the number of fields is 0 the line must be empty. In awk # 0 is false and non-zero is true. Thus when 'NF' is non-zero (non-empty) # it is true, and the line will be printed.
- jonathonf 12y agoInstead of: cat foo.txt | ruby -ne 'BEGIN{ $x=[]}; $x << $_.chomp; END { puts $x.sort }' just use `sort`: sort foo.txt There's a reason these commands already exist. :)