0

In my code, I am searching for a string and trying to print along with it, the two lines above it.

My command outputs to a text file file.txt which then contains multiple sequences of three distinct key pairs.

For example:

Principle: principle name 
profile:profile name
Authadd:CRT,DLT,PASSID
Principle: principle name 
profile:profile name
Authadd:CRT,CHG,PASSID
Principle: principle name 
profile:profile name
Authadd:CRT,INQ,PASSID
Principle: principle name 
profile:profile name
Authadd:CRT,PASSID

Authadd is the last in each sequence of three entries.

AUTHADD has many values like CRT,DLT,CHG,PASSID,SETALL, we have to only capture the entries which have CRT Or DLT and print the above two lines like Principle and Profile values.

I don't see any command, or code, for a Windows batch script to fetch the lines above the search string; like grep -a2 in Unix.

Can someone help me with the command, if any, to get the lines above the search string?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • If your 'command basically returns 3 fields', then why not just capture all three lines, then drop them all if the last line doesn't begin with `Authadd:` and is followed by the other two strings? I would presume that the reason is that your 'command' doesn't just return those three lines, and that information is important to anyone wishing to help. As is whether the order of `Authadd:CRT,DLT,` is consistent! Please therefore [Edit] your question to submit more representative examples of the actual output, and preferably the actual 'command' used. We also expect you to submit your 'code'. – Compo Aug 10 '23 at 11:32
  • yes, it has multiple entries with the 3 fields. and Authadd is the last field. – sachinrameshch Aug 10 '23 at 11:40
  • Your edit is simply not good enough. You have not done what I asked, and, as a result, it now appears as if you just want the last three lines of output! Please reread my previous comment, and try to help us to help you! – Compo Aug 10 '23 at 12:29
  • i have modified my question again . – sachinrameshch Aug 10 '23 at 13:46
  • I have improved your question again. Where is the code you'd like us to help you to fix a specific and reproducible issue with? BTW, this site is not a free code writing service! – Compo Aug 10 '23 at 15:21
  • Each of the answers of [this related question](https://stackoverflow.com/questions/17940198/batch-script-to-print-previous-and-next-lines-of-search-string-in-a-text-file?rq=2) should be easy to adapt to your needs. – Stephan Aug 10 '23 at 18:12
  • i have tried the ones suggested by stephan earlier,somehow they are not working and i am not an expert in Batch scripting to debug them . i have included both profile and principal in the findstr and it is serving the purpose for now like findstr "CRT DEL PROFILE PRINCIPAL" Thanks all for the suggestions – sachinrameshch Aug 11 '23 at 10:12

1 Answers1

0

This works!   With just one findstr command!!   ;)

@echo off
setlocal EnableDelayedExpansion

rem Create a "new line" (CR+LF) character pair:
for /F %%r in ('copy /Z "%~F0" NUL') do set ^"\n=%%r^
%Don't remove this line%
^"

rem Set the value to search for
set "search=DLT"

rem Lines before:      2                1           Base line
findstr /R ".*!\n!.*!\n!.*%search% .*!\n!.*%search% %search%" test.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108