简体   繁体   中英

Read file line by line in PowerShell taking a single string

I want to read a file line by line in PowerShell. Specifically, I want to take a value with the date "30-Jun-2020", however I only managed it with the entire line.

I know the command with get-content:

$GetData = (Get-Content .\file.dat -TotalCount 3)[-1]

But how do I get just a single value in the string?

SERVER SCTTP0012CLD 0050568E4146 7260
DAEMON server01.d D:\GCTI\LicenseManager1\server01.d.exe port=2345
FEATURE 3GP08590BCAA server01.d 8.0 30-jun-2020 18144 581E94E4546D \
vendor_info="v8.0 - server01 CIME Platform - MS" NOTICE="afric \

以下使用带有-replace运算符的正则表达式(正则表达式)从感兴趣的行中以dd-MMM-yyyy形式提取日期:

(Get-Content .\file.dat -TotalCount 3)[-1] -replace '^.+?\b(\d{2}-[a-z]{3}-\d{4})\b.+$', '$1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM