Get-Content, Set-Content, Select-String
Leggere, scrivere e cercare nel contenuto dei file di testo.
Get-Content (cat, type)
Get-Content .\file.txt
Get-Content .\file.txt -TotalCount 10
Get-Content .\file.txt -Tail 20
Get-Content .\file.log -Wait
Get-Content .\file.log -Tail 10 -Wait
Get-Content .\file.txt -Raw
Get-Content .\file.txt -Encoding UTF8
| Parametro | Significato |
|---|---|
-TotalCount N | Solo le prime N righe (come head) |
-Tail N | Solo le ultime N righe (come tail) |
-Wait | Resta in attesa di nuove righe (come tail -f); interrompi con Ctrl+C |
-Raw | Restituisce un'unica stringa invece di un array di righe |
-Encoding | Codifica del file |
Senza -Raw il risultato è un array con una riga per elemento: conviene per elaborare riga per riga.
Set-Content, Add-Content, Out-File
Set-Content .\file.txt "Prima riga"
Set-Content .\file.txt -Value "riga1","riga2"
Add-Content .\file.txt "Riga aggiunta"
"Testo" | Out-File .\file.txt
"Testo" | Out-File .\file.txt -Append
Get-Process | Out-File .\processi.txt
Clear-Content .\file.txt
| Cmdlet | Comportamento |
|---|---|
Set-Content | Scrive sovrascrivendo |
Add-Content | Aggiunge in coda |
Out-File | Scrive l'output formattato (con -Append aggiunge) |
Clear-Content | Svuota il file senza eliminarlo |
Anche gli operatori > e >> funzionano, ma in Windows PowerShell 5.1 salvano in UTF-16: meglio -Encoding utf8 con Out-File o Set-Content.
Modificare il contenuto
(Get-Content .\config.txt) -replace 'vecchio','nuovo' | Set-Content .\config.txt
(Get-Content .\config.txt -Raw) -replace '(?m)^#.*\r?\n','' | Set-Content .\config.txt
Racchiudere Get-Content tra parentesi fa leggere tutto il file prima di riscriverlo: indispensabile per scrivere sullo stesso file.
Select-String (grep)
Select-String -Path .\log.txt -Pattern "errore"
Select-String -Path .\*.log -Pattern "errore"
Select-String -Path .\log.txt -Pattern "errore" -CaseSensitive
Select-String -Path .\log.txt -Pattern "errore" -Context 2,2
Select-String -Path .\log.txt -Pattern "debug" -NotMatch
Select-String -Path .\*.txt -Pattern "pass" -List
Select-String -Path .\log.txt -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches
| Parametro | Significato |
|---|---|
-Pattern | Testo o espressione regolare (di default ignora maiuscole/minuscole) |
-CaseSensitive | Distingue maiuscole e minuscole |
-SimpleMatch | Cerca testo letterale, senza regex |
-Context a,b | Righe prima e dopo |
-NotMatch | Righe che NON corrispondono |
-List | Solo la prima corrispondenza per file |
-AllMatches | Tutte le corrispondenze nella riga |
Cercare ricorsivamente:
Get-ChildItem -Recurse -Filter *.php | Select-String "mysqli_connect"
Get-ChildItem -Recurse -File | Select-String "password" | Select-Object Path, LineNumber, Line
Estrarre solo la parte corrispondente (ad esempio gli indirizzi IP):
Select-String -Path .\log.txt -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches |
ForEach-Object { $_.Matches.Value } | Sort-Object -Unique
Contare, ordinare, rimuovere duplicati
(Get-Content .\file.txt).Count
Get-Content .\file.txt | Sort-Object
Get-Content .\file.txt | Sort-Object -Unique
Get-Content .\file.txt | Measure-Object -Line -Word -Character
Leggere file strutturati
Get-Content .\dati.json | ConvertFrom-Json
Import-Csv .\dati.csv
[xml](Get-Content .\dati.xml)