Can I use Powershell to automatically extract an unknown string with a specific pattern from a XML file and write that string in a text file?

Bob

In a XML file with 100 lines of code, there is one string with a specific pattern that I want to find and write into a new text file.

What the string contains is unknown and can vary, but the pattern is the same. For example:

12hi34

99ok45

Those have in common that the length is 6 and element:

0-1: integers

2-3: characters

4-5: integers

Is there a way to use Powershell and write a script that can find the string that fit the pattern and export it in a text file?

I'm new to Powershell and scripting. Tried to Google the problem and stumbled upon Select-String, but that doesn't solve my problem. Hope some of you can guide me here. Thanks.

Edit: The string is outside the root element as some "free text". It is not a traditional XML file.

mklement0

Assuming there's only one token of interest in the file, and that the letters are limited to English letters 'a' through 'z':

(Get-Content -Raw in.xml) -replace '(?s).*(\d{2}[a-z]{2}\d{2}).*', '$1' > out.txt

Note:

  • If no matching token is found, the input file's entire content is written to out.txt.

  • On Windows PowerShell > produces UTF-16LE ("Unicode") files by default (in PowerShell Core it is UTF-8 without a BOM); pipe to Set-Content out.txt -Encoding ... instead to create a file with a different encoding.

  • Get-Content -Raw reads the entire input file as a single string.

  • The -replace operator uses regular expressions (regexes) for matching - see this answer for more information.

    • Inline option (?s) at the start of regex makes . match newlines too.
    • By default, matching is case-insensitive; use -creplace for case-sensitive matching.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract string from text file via Powershell

Powershell extract string from file

How can I both extract a specific line in a text file as well as multiple lines containing a specific string?

Extract a specific text pattern from a string

How do I extract specific words from a string in a text file? c++

How I can extract specific target number from text file

extract, print and use a specific string from file line as a variable

How can I extract text before a character or string in a batch file?

String manipulation by specific delimiter and write in text file

How can I extract words from a file from a string

Count specific string in text file using PowerShell

Extract value from string in file powershell

Extract text from a specific pattern in text file using python

Extract specific information from string in a csv file

How I can sort a text file for specific string?

Can I extract a formatted string from a PDF File name in JavaScript?

How can I extract file extension from string?

Write String to a text file

How can I write a certain number of characters from a file to a string?

Extract a regex pattern from a specific column of a text file then perform a subtraction

How can I write a regex to extract data preceding and following a specific expression in a text file?

Read and write a String from text file

Bash extract string from file to use as variable

How can I delete specific string from a file

How can I extract an unknown substring from a string in Swift 4?

How can I Extract Specific xml tags from a local xml file using python?

Ubuntu: How do I extract only specific columns from tab-delimited file if it contains a specific string?

sed or awk extract values from output file column string pattern

Extract text from XML file