问题
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.
回答1:
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
回答2:
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.
回答3:
Using tac and sed:
tac file | sed -n '/Failed to execute/{n;p;}' | tac
回答4:
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'
回答5:
with PowerShell:
PS II> Select-String "Failed to execute:" c:\file.txt -co 1 | % { $_.context.Precontext }
回答6:
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"
)
来源:https://stackoverflow.com/questions/13216972/need-to-copy-the-lines-just-before-a-particular-string-using-batch-file