0

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 \
  • 2
    You could use regex or if the string you're after is always at the same position you could use string methods like `.substring()` or `-split`. – Olaf Mar 12 '20 at 01:36

1 Answers1

1

The following uses a regex (regular expression) with the -replace operator to extract a date in the form dd-MMM-yyyy from the line of interest:

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