Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy specific lines from a text file to separate file using powershell

Tags:

powershell

I am trying to get all the lines from an Input file starting with %% and paste it into Output file using powershell.

Used the following code, however I am only getting last line in Output file starting with %% instead of all the lines starting with %%.

I have only started to learn powershell, please help

$Clause = Get-Content "Input File location"
$Outvalue = $Clause | Foreach { 
    if ($_ -ilike "*%%*")
    {
        Set-Content "Output file location" $_
    }
}
like image 693
Jabir Jamal Avatar asked Jan 04 '23 23:01

Jabir Jamal


2 Answers

You are looping over the lines in the file, and setting each one as the whole content of the file, overwriting the previous file each time.

You need to either switch to using Add-Content instead of Set-Content, which will append to the file, or change the design to:

Get-Content "input.txt" | Foreach-Object { 
    if ($_ -like "%%*") 
    {
        $_     # just putting this on its own, sends it on out of the pipeline
    }
} | Set-Content Output.txt

Which you would more typically write as:

Get-Content "input.txt" | Where-Object { $_ -like "%%*" } | Set-Content Output.txt

and in the shell, you might write as

gc input.txt |? {$_ -like "%%*"} | sc output.txt

Where the whole file is filtered, and then all the matching lines are sent into Set-Content in one go, not calling Set-Content individually for each line.

NB. PowerShell is case insensitive by default, so -like and -ilike behave the same.

like image 76
TessellatingHeckler Avatar answered Jan 07 '23 13:01

TessellatingHeckler


For a small file, Get-Content is nice. But if you start trying to do this on heavier files, Get-Content will eat your memory and leave you hanging.

Keeping it REALLY simple for other Powershell starters out there, you'll be better covered (and with better performance). So, something likes this would do the job:

$inputfile = "C:\Users\JohnnyC\Desktop\inputfile.txt"
$outputfile = "C:\Users\JohnnyC\Desktop\outputfile.txt"

$reader = [io.file]::OpenText($inputfile)
$writer = [io.file]::CreateText($outputfile)

while($reader.EndOfStream -ne $true) {
    $line = $reader.Readline()
    if ($line -like '%%*') {
        $writer.WriteLine($line);
    }
}

$writer.Dispose();
$reader.Dispose();
like image 33
João Ciocca Avatar answered Jan 07 '23 13:01

João Ciocca