Powershell-如何从文本文件中获取前几行? [英] Powershell - how to obtain previous lines from text file?
问题描述
我有一个文本文件,其中包含数百行包含数据库信息的文本.
I have a text file containing hundreds of lines of text containing database info.
我正在尝试为任何35GB的数据库提取DatabaseId
.
I'm trying to extract the DatabaseId
s for any database which is 35GB.
我想使用Powershell询问该文件,并生成一个包含所有匹配数据库的文本文件.
I would like to interrogate the file using Powershell and produce a text file containing all the matching databases.
因此,从本质上讲,我想浏览文件,找到35的DatabaseSize
,然后从前3行中提取相应的DatabaseId
.
So in essence, I would like to scan through the file, find a DatabaseSize
which is 35 and then extract the corresponding DatabaseId
from 3 lines previous.
我看了遍整个网络,但似乎找不到能做到这一点的任何东西.
I've looked all over the net but can't seem to find anything which can do this.
文本文件摘录示例:
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
推荐答案
尝试使用以下方法:
(( GC myfile.txt |
Select-String 'DatabaseSize = 35' -Context 3 ).context.precontext)[0]
如果有多个匹配项:
(GC myfile.txt |
SELECT-STRING 'DATABASESIZE = 35' -Context 3 ) | % { ($_.context.precontext)[0] }
这篇关于Powershell-如何从文本文件中获取前几行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!