user1797693
user1797693

Reputation:

Need to copy the lines just before a particular string using batch file

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.

Upvotes: 0

Views: 366

Answers (6)

walid2mi
walid2mi

Reputation: 2888

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"
)

Upvotes: 0

walid2mi
walid2mi

Reputation: 2888

with PowerShell:

PS II> Select-String "Failed to execute:" c:\file.txt -co 1 | % { $_.context.Precontext }

Upvotes: 0

dbenham
dbenham

Reputation: 130819

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.

Upvotes: 3

knittl
knittl

Reputation: 265171

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

Upvotes: 2

knittl
knittl

Reputation: 265171

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'

Upvotes: 1

Guru
Guru

Reputation: 16974

Using tac and sed:

tac file | sed -n '/Failed to execute/{n;p;}'  | tac

Upvotes: 1

Related Questions