0

I have a file like this:

Executing resource: D:\waste2\SPC\depks_rtl_teller_custom.spc   
Executing resource: D:\waste2\SPC\ifpks_msg_incoming_cluster.spc  
Failed to execute:   
Executing resource: D:\waste2\SQL\casapks_batch.sql  
Failed to execute:  
Executing resource: D:\waste2\SQL\depks_decbatop_kernel.sql  
Executing resource: D:\waste2\SQL\depks_services.sql  
Failed to execute:    

I need a batch file or perl script or ANT script to pick all the lines just in front of the string "Failed to execute:" and copy to a new file. Simply the failed file list I need in a new file. Please assist.

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185

6 Answers6

3

Surprise! The native Windows FINDSTR command can handle this problem quite nicely :-) There is no need for perl, or any other non-native utility.

@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 /rc:"!CR!*!LF!Failed to execute:" "test.txt" >"failed.txt"
type failed.txt

See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

With perl, you could do something like:

while(<>) {
  print $prev if /^Failed to execute:/;
  $prev = $_;
}

To execute directly from your shell, you can use the following command

perl -ne 'print $prev if /^Failed to execute:/; $prev=$_' path/to/your/file
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Hi Knittl, Thanks for the update, I am not so experienced in perl. Suppoese my source file is a.txt with the content as mentioned above, and I need b.txt with the Failed files. Could you please help. Thanks in advance. –  Nov 04 '12 at 12:10
  • @user1797693: Just redirect output to the new file: `perl -ne '...' a.txt >b.txt` – knittl Nov 04 '12 at 12:11
1

Using tac and sed:

tac file | sed -n '/Failed to execute/{n;p;}'  | tac
Guru
  • 16,456
  • 2
  • 33
  • 46
1

You could also use two grep invocations, although this is more of a hack (assuming you only have lines starting with either "failed" or "executing"):

grep -B1 '^Failed to execute' your/file | grep '^Executing'

Or

grep -B1 '^Failed to execute' your/file | grep -v '^--' | grep -v '^Failed to execute'
knittl
  • 246,190
  • 53
  • 318
  • 364
0

with PowerShell:

PS II> Select-String "Failed to execute:" c:\file.txt -co 1 | % { $_.context.Precontext }
walid2mi
  • 2,704
  • 15
  • 15
0

or simply:

for /f "delims=" %%a in (c:\test.txt) do (
  echo(%%a| find /i "Failed to execute:" >Nul && (
    setlocal enableDelayedExpansion
    echo !msg!
    endlocal
  )
  set "msg=%%a"
)
walid2mi
  • 2,704
  • 15
  • 15