1

假设我有一个这样的文本文件:

SELL USDCAD 0.99257 0.99653 0.39 -150.1 -8.57% -580.99 1 week ago  Copy 
SELL USDCAD 0.98986 0.99653 0.26 -177.2 -6.75% -457.25 1 week ago  Copy 
SELL USDCAD 0.98711 0.99653 0.17 -204.7 -5.10% -345.37 1 week ago  Copy 
BUY USDCAD 0.98613 0.98715 0.17 8.4 0.21% 14.47 1 week ago  Copy 

我正在寻找其中包含“1 周前”这个字符串的行/行。并将其带到一个变量并写入另一个文本文件。

我怎样才能做到这一点?

4

3 回答 3

2

is there any reason you have to use ruby for this, when one line from the command line will do the same thing?

grep '1 week ago' file.txt > newfile.txt
于 2013-01-25T20:59:02.990 回答
0

使用以下命令从命令行执行此操作:

ruby -ne 'print if $_["1 week ago"]' < file_to_read > file_to_write
于 2013-01-25T21:01:25.487 回答
0

你可以这样做:

str = <<EOT 
SELL USDCAD 0.99257 0.99653 0.39 -150.1 -8.57% -580.99 1 week ago  Copy 
SELL USDCAD 0.98986 0.99653 0.26 -177.2 -6.75% -457.25 1 week ago  Copy 
SELL USDCAD 0.98711 0.99653 0.17 -204.7 -5.10% -345.37 1 week ago  Copy 
BUY USDCAD 0.98613 0.98715 0.17 8.4 0.21% 14.47 1 week ago  Copy
EOT

content = str.lines.select { |l| l.include? '1 week ago' }.join("\n")
File.open('asdf.txt', 'w') { |file| file.write content }
于 2013-01-25T20:56:38.040 回答