Powershell - how to get previous lines from a text file?

I have a text file containing hundreds of lines of text containing database information.

I am trying to extract DatabaseId for any database that is 35 GB.

I would like to poll the file using Powershell and create a text file containing all the relevant databases.

So basically, I would like to scan the file, find DatabaseSize , which is 35, and then extract the corresponding DatabaseId from the previous three lines.

I looked through the entire network, but I can not find anything that can do this.

Example of extracting a text file:

 ServerId = VMlinux-b71655e1 DatabaseId = db-ecb2e784 Status = completed LocationId = 344067960796 DatabaseSize = 30 ServerId = VMlinux-0db8d45b DatabaseId = db-cea2f7a6 Status = completed LocationId = 344067960796 DatabaseSize = 35 ServerId = VMlinux-c5388693 DatabaseId = db-9a421bf2 Status = completed LocationId = 344067960796 DatabaseSize = 8 etc 
+4
source share
1 answer

Try something like this:

 (( GC myfile.txt | Select-String 'DatabaseSize = 35' -Context 3 ).context.precontext)[0] 

In case of multiple matches:

 (GC myfile.txt | SELECT-STRING 'DATABASESIZE = 35' -Context 3 ) | % { ($_.context.precontext)[0] } 
+6
source

Source: https://habr.com/ru/post/1481184/


All Articles