0

次のような source.txt ファイルがあります。

Executing resource: L:\Core\SQL\smpks_fcj_csdovdmt_addon.sql  
Failed to execute: 
Executing resource: L:\LC\SQL\lcpks_lcdtronl_utils.sql  
Executing resource: L:\LS\SQL\cspks_adhoc_charge_sch.sql  
Failed to execute:   
SHO ERR  
Executing resource: L:\LS\SQL\gwpks_ls_adhocsch_operations.sql  
Executing resource: L:\LS\SQL\lnpks_synd_services.sql  
Failed to execute:  
Executing resource: L:\LS\SQL\lspks_facility_contracts.sql  
Executing resource: L:\LS\SQL\lspks_fcj_lsdpxlog.sql  
Executing resource: L:\LS\SQL\lspks_lsdprmnt_kernel.sql  
Failed to execute:  
sho err;

このファイルから、「Failed to execute:」という文字列の直前の行を取得し、それを新しいファイル result.txt に入れる必要があります。もう 1 つの条件として、「Failed to execute:」の後に「sho err:」または「SHO ERR」が続く場合、結果にそのファイルは必要ありません。上記のファイルの出力は次のようになります。

Executing resource: L:\Core\SQL\smpks_fcj_csdovdmt_addon.sql  
Executing resource: L:\LS\SQL\lnpks_synd_services.sql

助けてください。Perl スクリプトまたはバッチ ファイルまたは Ant スクリプトで問題ありません。

「Sho err」がある場合、前の行を削除するなどのオプションが表示された場合は、問題ありません。

4

4 に答える 4

2

バッチ ファイルを使用して特定の文字列の直前に行をコピーする必要がある にあるネイティブ バッチ ソリューションを拡張します。

Failed to execute:最初に FINDSTR を使用して、後に続くものをすべて削除しSHO ERR、結果を別の FINDSTR にパイプして、後に続く残りの行を保存します。Failed to execute:

@echo off
setlocal
::Define LF variable containing a linefeed (0x0A)
set LF=^


::Above 2 blank lines are critical - do not remove

::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

setlocal enableDelayedExpansion
::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
findstr /vbric:"Failed to execute:.*!CR!*!LF!SHO ERR" "test.txt" ^
 | findstr /ric:"!CR!*!LF!Failed to execute:" >failed.txt

type failed.txt
于 2012-11-09T12:48:45.103 に答える
2

これは私のために働く:

grep -B 1 "Failed" sources.txt  | grep "Executing resource:" > results.txt

編集: 彼の「SHO ERR」要件を考慮に入れるには、おそらく Perl を使用する方が良いでしょう:

my @lines = <>;
push @lines, "";
push @lines, "";
my $linesNum = @lines;
for ( $i = 0; $i < $linesNum - 2; $i++) {
   if ($lines[$i+1] =~ /Failed to execute:/) {
     if (!($lines[$i+2] =~ /SHO ERR/i)) {
        print $lines[$i] if $lines[$i] =~ /^Executing/
     }
   }
 }

次のように使用できます。

cat sources.txt | perl script.pl > results.pl

また

perl script.pl sources.txt > results.pl
于 2012-11-09T09:08:44.797 に答える
1

tacとsedの使用:

$ tac file | sed -n  -e '/sho err\|SHO ERR/{N;d;}' -e '/Failed to execute/{n;p;}' | tac
Executing resource: L:\Core\SQL\smpks_fcj_csdovdmt_addon.sql
Executing resource: L:\LS\SQL\lnpks_synd_services.sql
于 2012-11-09T10:12:05.713 に答える
1

ちょうど別のPerlソリューション:

#!/usr/bin/perl

use 5.012; use warnings;

my $last = "";
my $commit_ready = 0;

while (<>) {
  if (/^Failed to execute:/) {
    $commit_ready = 1;
  } else {
    print $last if $commit_ready and not /^sho err/i;
    $commit_ready = 0;
    $last = /^Executing resource: (.*)/ ? $1 : "";
  }
}
print $last if $commit_ready;

使用法は@sergioスクリプトと同じです。

于 2012-11-09T09:52:53.953 に答える