开发者

Powershell - grab a word and emit it (or something)

I need to read from a file and find a word in it. The file is generated new each time my script is run. The file has many lines in it, but there is always one unique line which has the following:

INSTANCE i-cbea4fa5 ami-c3e4dda  ec2-........

The values "INSTANCE" and "ami-c3e4dda" will always be th开发者_如何学编程e same in the file. The second item, "i-cbea4fa5" will vary but will always be in the same location within that line, and will always begin with "i-".

I need to find and then use the "i-cbea4fa5" value in a script.

1) how can I pull out just that "i-cbea4fa5" value? 2) once found, what is the best way to use it?

Thank you, Daniel Williams


Use a regular expression, then extract given part of a resulting string. Example command (there are many ways to do it, but Select-String cmdlet is essential) below. I assumed "i-..." part is always a single word (i.e. no white characters). Also, if multiple values found, the first one is returned.

(Select-String -path file.txt -pattern "^INSTANCE i-.+ ami-c3e4dda" -AllMatches | % { $_.Matches } | % { $_.Value })[0].Split()[1]


((select-string <filename> -pattern "^INSTANCE \S+ ami-c3e4dda" -list).line).substring(9,10)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜