搜索模式.把它放在缓冲区中,并使用正则表达式(记事本++或cygwin Shell)或JSFiddle进行排序



我想识别某些模式并将整行移动到文件的特定部分,从而重新排列文件内容。我更喜欢notepad++的解决方案,但如果您认为这太复杂了,那么cygwin shell ( awk )JSfiddle也可以 我将在下面用例子来说明我的观点

I have a pattern that is 
"col<variable space>stat<variable space>col ( axx,bvb,ccc) on mr.dan"  (<some word> confidence)
e.g. 
"col  stat  col ( a123,b6949,c4433) on Mr.Randy"  (Low confidence) 
"col         stat       col     ( a1fddf23, b6ff949,c4433 ) on    John.Doe  "  (Low confidence) 
"col     stat   col     ( ax ) on    John.Dane  "  (Ok confidence) 
"col stat col ( axdf,fsdds ) on    Jane.Dame "  (  Fair confidence ) 

它应该做什么

  • 去掉所有的引号,去掉(<word> confidence)的部分,在行尾贴一个";"(我可以管理这部分,这里不需要帮助)
  • 与模式
    的表达式col ( axdf,fsdds )

cols+(s*word1s*,s*word2s*,s*wordNs*)s*ons*word.words*


上面的模式需要重新排列,以便一个单词col ( word)的将排在顶部,然后是两个单词col ( word1, word2),依此类推,按col ( word )表达式中单词数的升序排列
所以输出以上应该是

col     stat   col     ( ax ) on    John.Dane  ;    # 1 word in col (word) expr 
col stat col ( axdf,fsdds ) on    Jane.Dame ;     # 2 words in col (word) expr 
col         stat       col     ( a1fddf23, b6ff949,c4433 ) on    John.Doe  ;    ; # 3 words in col (word) expr 
col  stat  col ( a123,b6949,c4433) on Mr.Randy; 

我做了什么
,我可以使用"s*((s*(w+)*s*Confidence))替换为;

我需要第二部分col ( word)表达式重新排列的帮助。
Notepad++的逻辑伪代码将是前两个将每个列表达式中的单词列表隔离在单独的缓冲区中。 接下来,计算每个缓冲区中的单词数,然后排列缓冲区。 根据缓冲区排列,您可以排列表达式。
也对JsFiddleShellscript regex / awk开放

这不能用记事本++完成,我建议使用脚本,这里有一个Perl脚本的例子来完成这项工作。

整个文件都在内存中读取,如果文件非常大,那将是一个问题。

#!/usr/bin/perl
use Modern::Perl;
# Read input file in an array
my $file_in = 'file.txt';
open my $fh, '<', $file_in or die "unable to open '$file_in': $!";
my @lines = <$fh>;
# Replace last quote until end of line with semicolon and remove quotes
my @unsorted = map { s/"[^"]*$/;/; s/"//g; $_ } @lines; 
# use Schartzian transform for sorting
my @sorted = 
# remove the number of words
map  { $_->[0] }
# sort on number of words
sort { $a->[1] <=> $b->[1] }
# Add number of words
map  { 
# list of words inside parenthesis
my ($words) = $_ =~ /(([^)]+))/;
# split to have number of words
my @w = split',', $words;
# add this number as second element in array
[$_, scalar @w] 
}
@unsorted;
# Write into output file
my $file_out = 'file_out.txt';
open my $fh_out, '>', $file_out or die "unable to open '$file_out': $!";
say $fh_out $_ for @sorted;

输出文件:

col     stat   col     ( ax ) on    John.Dane  ;
col stat col ( axdf,fsdds ) on    Jane.Dame ;
col  stat  col ( a123,b6949,c4433) on Mr.Randy;
col         stat       col     ( a1fddf23, b6ff949,c4433 ) on    John.Doe  ;

最新更新