0

我有一个需要从中提取特定模式的日志文件。我需要找到它们,然后将它们处理成一个新文件。Linux 上的 grep 通常可以解决问题,但正则表达式跨越多行,我知道 grep 不会这样做。

这是我的日志/调试文件中的一个示例:


Da:
1.328   0.5045  

Db:
0.6415  0.1192  

Lambda:
0.4429  -0.35   
-0.0461 -0.02421    

seps:
0.714272

我正在寻找/Lambda:\n([-\d\.]+)\s+([\-\d\.]+)\s+\n([\-\d\.]+)\s+([\-\d\.]+)/ 然后我想将这些行输出到一个新文件中删除 lambda 并将数字重新排列到同一行以便输出\1\s\2\s\3\s\4\n

所以我实际上有两个问题:

  1. 在任何系统上是否有一个简单的实用程序可以实现这一点?
  2. 有没有办法专门在 Windows 上执行此操作?

我希望有一个简单的解决方案可以解决这个问题。我宁愿呆在 Windows 中,但如果我必须使用 Linux,我会完成这项工作。

4

3 回答 3

1

这是awk,perl和类似状态解析的一个很好的候选者(这些将在 Windows 中运行CMD.EXE,前提是你有perl和/或awk/sed在你的PATH,当然,在 Linux 和其他 unices 上):

awk "/^Lambda/ { in_lambda=1 ; next } in_lambda && /^ *$/ { in_lambda=0 ; printf \"\n\" ; next } in_lambda { printf \"%s \", $0 }" input_file >output_file

或者

perl -ne "chomp; if (/^Lambda/) { $in_lambda = 1 } elsif ($in_lambda && /^ *$/) { $in_lambda=0 ; printf \"\n\" } elsif ($in_lambda) { printf \"%s \", $_ }" input_file >output_file


如果需要,您可以执行第二遍以规范化空白(并在行尾修剪空白)。

awk "/^Lambda/ { in_lambda=1 ; next } in_lambda && /^ *$/ { in_lambda=0 ; printf \"\n\" ; next } in_lambda { printf \"%s \", $0 }" input_file| sed -e "s: *: :g" -e "s: *$::" >output_file

或者

perl -ne "chomp; if (/^Lambda/) { $in_lambda = 1 } elsif ($in_lambda && /^ *$/) { $in_lambda=0 ; printf \"\n\" } elsif ($in_lambda) { printf \"%s \", $_ }" input_file| perl -ne "s/ +/ /g; s/ +$//g; print" >output_file

于 2009-02-21T04:19:25.370 回答
0

您可以安装 Perl 或 Python 或 Ruby 或 PHP 并相当容易地编写脚本。

于 2009-02-21T04:07:45.460 回答
0

感谢所有的答案。我喜欢你给我的关于 perl 和 awk 的答案。我是那些不知道 perl 的奇怪程序员之一,所以我选择了 ruby​​ 路线。这是我的解决方案

x=ARGV[0]
f=File.new(ARGV[1])
g=File.new(ARGV[2],"w")
f.read.gsub(/#{x}:\s*(([\d\.\-]*\t*)+\n)+/ ){|entry|
    puts entry
    g.puts entry.gsub(/#{x}:\n/,'').gsub(/\s+/,"\t").strip
}

据我所知,我可以通过 NppExec 将它用作我的编辑器 Notepad++ 的实用程序,它不支持重定向和管道。这也允许我收集我需要通过程序诊断的任何输出。再次感谢大家。

于 2009-03-02T16:52:38.697 回答