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 \
1
  • 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. Commented Mar 12, 2020 at 1:36

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.