batch file to find a word, extract 2 line above the word and write it in diffrent text file

0

In my scenario i need to check my file (XXX.txt) for the word "SPOT". There will be few of SPOT word in the file.

Now i need a script to write the top 2 line of the "SPOT" word in a new text fine and name it as "done.txt".

EXAMPLE XXX.txt

dfsfs
clip_id HDX
title Setiap hari
type PROMO
end
clip_id HDQ
title Citra
type SPOT
end
clip_id HDX
title Ri
type PROMO
end
clip_id HDX
title sni
type SPOT

so i need to write "done.txt" as per below.

clip_id HDQ
title Citra

clip_id HDX
title sni

Please advise for script to do this.

Many thanks

Poobalan

Posted 2015-03-19T07:14:23.500

Reputation: 1

Welcome to Super User. Unfortunately, we are not a code-writing service. Instead of simply asking for code to perform a particular task, please show us what you've tried so far (including any code you currently have) and where you're stuck so that we can help you with your specific problem. Questions that only ask for code are too broad and are likely to be put on hold or closed

– DavidPostill – 2015-03-19T07:42:17.900

For a pure CLI .batsolution, you could start with comparing output from findstr /n /i "spot" "xxx.txt" command and from findstr /n /R $ "xxx.txt" and then treat it in two nested FOR /F loop commands. Feel free to return here and [edit] your question following @DavidPostill comment hint.

– JosefZ – 2015-03-19T09:34:22.527

Answers

0

Here's the solution in Powershell, Microsoft's cmd successor:

# variables that you need to set
$source = "C:\temp\superuser.txt"
$destination = "C:\temp\done.txt"

#region easier to understand for PS newcomers
$hits = select-string -Path $source -SimpleMatch "SPOT" -CaseSensitive
$filecontents = get-content $source

foreach($hit in $hits)
{
    $filecontents[$hit.linenumber-3] | out-file -append $destination
    $filecontents[$hit.linenumber-2] | out-file -append $destination
    "" | out-file -append $destination
}
#endregion


#region shorter version
$hits = select-string -Path $file -SimpleMatch "SPOT" -CaseSensitive -context 2

foreach($hit in $hits)
{
    $hit.context.PreContext | out-file -append $destination
    "" | out-file -append $destination
}
#endregion

megamorf

Posted 2015-03-19T07:14:23.500

Reputation: 1 494

hi megamorf thnak you for the coding. But should I save this as .bat or .ps1 file? i'm a newbie and i done see any output if run as .bat...please enlighten me..many thanks! – Poobalan – 2015-03-19T09:48:45.247

Save the script as .ps1, You can run it from Powershell by simply entering the path to the script or run it via batch like so: powershell -executionpolicy bypass C:\temp\myscript.ps1 – megamorf – 2015-03-19T09:54:25.990

Windows PowerShell Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\pmpooba\Desktop\SPOT COMMERCIAL> test4 The term 'test4' is not recognized as the name of a cmdlet, function, script fi le, or operable program. Check the spelling of the name, or if a path was inclu ded, verify that the path is correct and try again. At line:1 char:6

  • test4 <<<<
    • CategoryInfo : ObjectNotFound: (test4:String) [], CommandNotFou
    ndException
    • FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\pmpooba\Desktop\SPOT COMMERCIAL>------->this are the output :( – Poobalan – 2015-03-19T10:05:36.463

I assume your script is called test4? Call it like this .\test4.ps1 Powershell expects you to reference scripts or binaries with their file extension and a relative or absolute path for security reasons. Someone could just place a malicious test4.exe in the same directory and it would be called instead if that security measure wasn't in place. – megamorf – 2015-03-19T10:06:57.240

yeah it's cant be executed because some security policy...can save same script as .bat? if no..where should i make changes....millions of thanks – Poobalan – 2015-03-19T10:32:00.237

You can set the Powershell execution policy to remotesigned (Set-Executionpolicy remotesigned -scope currentuser) which enables you to run locally created scripts but prevents you from running scripts downloaded from the internet. – megamorf – 2015-03-19T10:40:26.417

0

I highly recommend JREPL.BAT - a hybrid JScript/batch utility that performs regular expression text replacement. JREPL.BAT is pure script that runs natively on any Windows platform from XP onward.

Assuming JREPL.BAT is in your current directory, or better yet, somewhere within your PATH, then, a simple command line one-liner efficiently solves your problem:

jrepl "^((.*\n){2})type SPOT$" $1 /m /jmatch /f xxx.txt /o done.txt
  • The /M multiline option allows searches accross newlines.
  • The /JMatch option prints only the replacement strings for matches

Since JREPL is a batch script, you must use CALL JREPL if you use it within another script.

Full documentation is available from the command line using jrepl /?. You may want to use jrepl /?|more to see the help one page at a time. I never use MORE because my cmd.exe console window is configured with a large output buffer that enables me to scroll up to see past output.

dbenham

Posted 2015-03-19T07:14:23.500

Reputation: 9 212

hi..this is wat i done..but failed..@echo on

setlocal enabledelayedexpansion

set src=C:\Users\pmpooba\Desktop\PGM\xxx.txt set dst=C:\Users\pmpooba\Desktop\PGM\done.txt

jrepl "^((.*\n){2})type SPOT$" $1 /m /jmatch /f %src% /o %dst%

) pause – Poobalan – 2015-03-27T09:57:30.907

I tested my code, and it works. You have an extra ) at the end of the JREPL command that causes it to fail. Also, You must put CALL before JREPL if you want the script to continue - otherwise your PAUSE command will not fire. – dbenham – 2015-03-27T11:38:34.227