Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Get only numeric value in text file using powershell?

I have a text file sample.txt containing

computer
computer.pc = 1
pc

i want only number 1, where i want to assign that value to a variable

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$number = Get -content "sample.txt"

>Solution :

You can extract the number by using the Regex Match method.

Example code to do this:

$number = ([regex]::Match((Get-content "sample.txt"), "\d+")).Value

The pattern \d+ means to match one or more decimal digits and using the Match method will return the first match found.

See Quantifiers in Regular Expressions for additional information regarding the quantifiers available.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading