Équivalent PowerShell de Grep

Si vous êtes à la recherche d’une commande similaire à Grep (Linux) sur Powershell. La commande Select-String peut vous permettre de faire la même action.

Pour Grep un fichier texte

Select-String -Path « C:\file_test\votre_fichier.txt » -Pattern ‘Mac’

Vous pouvez également utiliser un * au lieu d’un nom de fichier, ce qui permettra de rechercher tous les fichiers portant l’extension .txt ou une extension de votre choix.

         Select-String -Path « C:\file_test\*.txt » -Pattern ‘Mac’

Comme avec grep, vous pouvez faire un pipe vers Select-String comme ceci

Get-Content « C:\Files\fichier.txt » |Select-String « mac »

 

Si vous êtes habitué à la commande Grep ou si vous n’aimez pas la commande Select-String 🙂 vous ajouter un alias à la cmdlet Select-String 

         Set-Alias -Name grep -Value Select-String

 

Et Voilà !

cat « C:\Files\fichier.txt » | grep « mac »

Src:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7.1

Laisser un commentaire