How can I extract strings from some text file with powershell script?

Thm Lee

I wanted to extract some strings from some text files. After some researching for that files, I found some pattern that strings appear in a text file.

I composed a short powershell script by help of google-search. This script receives two parameters (textfile path and extracting keyword) and operates extracting strings from text file.

As finding & extracting the target strings from the file $tpath\temp.txt, this script saves it to another file $tpath\tmpVI.txt.

Set-PSDebug -Trace 2 -step
$txtpath=$args[0]
$exkey=$args[1]
$tfile=gc "$tpath\temp.txt"
$savextracted="$tpath\tmpVI.txt"

$tfile -replace '&', '&' -replace '^.*$exkey', '' -replace '\s.*$', '' -replace '\\.*$','' | out-file "$savextracted" -encoding ascii

But until now, the extracted & saved result has been fault, never wanted strings.

By PS debugging, it seems the regular expressions in the last line make troubles and variable $exkey does so in replace quotation. But I don't know how to fix this. What shall I do?

Maximilian Burszley

If you're looking to capture lines that have your match, here's a snippet that solves that problem:

Function Get-Matches
{
    Param(
        [Parameter(Mandatory,Position=0)]
        [String] $Path,

        [Parameter(Mandatory,Position=1)]
        [String] $Regex
    )

    @(Get-Content -Path $Path) -match $Regex
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract the text between two strings in a log file?

How can I extract specific strings from a text file and add to List?

How can I create a file with some text in it using bash script

How can I extract these characters from a text file?

How can I extract data from text file?

How can I extract a text from a bytes file using python

How I can extract specific target number from text file

How can I extract a portion of text from all lines of a file?

How can I convert strings from a text file into matrix (python)

How can I extract the data from these strings?

How can I extract some patterns of sub text from a gibberish looking text using regular expressions?

How to extract multiple strings from html code using PowerShell script

How can I extract strings from one file to insert (modified) in a different file?

How can I find strings that are not in a text file?

How I can extract just some fields from a CSV line of text

How can I extract text from images?

How can I extract the value of html TH tags that occur multiple times in a text file using a bash script?

how can I get a some values in strings in bash script

How can I extract the values from this Hashtable-like text in Powershell?

How can I extract records from a file if they include a specific set of strings?

python - How to extract strings from each line in text file?

How do I extract the text from a docx file using apps-script?

How can I copy some strings from file to another using c programming

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?

How can I use Powershell to extract mail headers from .msg file?

How can I extract text from multiple web pages of which some pages have text under different tags?

How would I extract the user agent strings from a log file?

How can I extract numbers containing commas from strings in python

How can I extract hashtags from strings and export as a csv?