我有这个文件:
1
2
3
4
a
b
c
XY
Z
字符串
我想将每个块转换为TAB
分隔行,并将当前timestamp
附加在最后一列,以获得如下输出:
我可以使用awk
来这样做:
awk '$(NF+1)=systime()' RS= OFS="\t" file
型
其中空RS
等同于set RS="\n\n+"
。
但是我想用Ruby一行程序来实现。我想到了这个
ruby -a -ne 'BEGIN{@lines=Array.new}; if ($_ !~ /^$/) then @lines.push($_.chomp) else (puts @lines.push(Time.now.to_i.to_s).join "\t"; @lines=Array.new) unless @lines.empty? end; END{puts @lines.push(Time.now.to_i.to_s).join "\t" unless @lines.empty?}' file
型
这有点尴尬
有没有什么优雅的方法来做到这一点?ruby
是否与awk
的RS
、NF
和OFS
等价?
1条答案
按热度按时间8wigbo561#
字符串
-00
段落模式-a
自动拆分,可从$F
阵列获得结果-l
chomps记录分隔符