Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_wordproof_timestamp is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$newsletter_subject is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_newsletter_subject is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$newsletter_summary is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_newsletter_summary is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
grep, quali alternative usare in Windows | IlSoftware.it

grep, quali alternative usare in Windows

Il comando grep in Linux e macOS è uno dei più conosciuti: permette di trovare le occorrenze di una stringa di testo in qualunque file. Quali alternative è possibile usare in ambiente Windows.

Il comando grep è uno strumento essenziale per la ricerca di testo all’interno del contenuto di file o di stream di input. Il suo nome deriva da Global Regular Expression Print: riflette la sua capacità di cercare pattern complessi in grandi quantità di testo.

grep è un’utilità da riga di comando disponibile nei sistemi Unix-like, in Linux e macOS. Assente in Windows, è progettata per cercare testo che soddisfi i criteri contenuti in un’espressione regolare o in una stringa semplice.

Come funziona il comando grep

Quando si usa grep, il comando legge l’input riga per riga e stampa le righe che contengono il pattern specificato dall’utente.

Ad esempio, grep "errore" log.txt cerca la parola errore nel file dal nome indicato (in questo caso potrebbe essere un file di registro).

Ancora, grep -r "TODO" /percorso/progetto cerca la stringa TODO in tutti i file contenuti nella directory specificata e nelle sue sottocartelle: l’opzione specificata realizza di fatto una ricerca ricorsiva.

Con il comando grep -c "warning" error_log.txt è addirittura possibile contare le occorrenze della parola warning nel file chiamato error_log.txt.

La semplice regular expression grep "^start" file.txt aiuta a trovare tutte le righe che iniziano con start in file.txt.

Il comando grep offre anche la possibilità di ignorare maiuscole/minuscole, di invertire la ricerca (visualizza le righe non corrispondenti alla stringa specificata), di esporre il numero di riga per ciascuna corrispondenza, di mostrare solo i nomi dei file con corrispondenze.

Cosa usare in Windows come alternativa a grep

La finestra del terminale Linux è potente perché permette di eseguire in un battito di ciglia operazioni che dall’interfaccia grafica sarebbero complesse e noiose da portare a termine.

Windows offre comunque alcune alternative al comando grep. Di seguito vediamo i migliori approcci che possono essere adottati.

Comando findstr

L’opzione più ovvia consiste nell’utilizzo dello storico findstr dal prompt dei comandi. Ad esempio, l’istruzione seguente cerca la presenza della stringa errore nel file log.txt:

findstr "errore" log.txt

Il comando seguente permette di cercare nei file della directory corrente e in tutte le sottocartelle (/S) ignorando la distinzione tra maiuscole e minuscole (/I):

findstr /S /I "errore" *.txt

Cmdlet PowerShell

Un’alternativa più efficace consiste nell’utilizzo della cmd PowerShell Select-String. L’istruzione che segue permette di cercare la stringa specificata nel file di testo:

Select-String -Pattern "errore" -Path "log.txt"

Con questa sintassi, si possono anche effettuare ricerche più complesse. Ad esempio, nella cmdlet che segue la stringa “403 error” è cercata per intero e non spezzata in due parti come fa invece findstr:

Select-String -Pattern "403 error" -Path "log.txt"

PowerShell indica non solo il nome del file contenente ogni occorrenza ma anche il numero di riga in cui appare.

Alternative grep Windows

La cmdlet seguente consente di cercare le occorrenze della stringa indicata in tutti i file con estensione .txt. I file possono essere contenuti nella directory indicata così come nelle eventuali sottocartelle.

Get-ChildItem -Path "D:\Documenti" -Filter *.txt -Recurse | Select-String -Pattern "403 error"

Usare Notepad++ per cercare all’interno dei file

Il popolare editor di testo Notepad++ integra una pratica funzione per cercare testi all’interno dei file memorizzati sul sistema.

Per procedere in tal senso, basta cliccare sul menu Ricerca, su Trova nei file quindi inserire la stringa da cercare nel campo Trova.

In corrispondenza di Filtri, si può ad esempio specificare *.txt mentre nel campo Cartella, va indicata la directory in cui cercare.

Cercare nei file con Notepad++

Con un clic su Trova tutti, Notepad++ mostra l’elenco di tutti i file contenenti la stringa da trovare.

Accedere al file system di Windows da WSL e usare grep

Windows 10 e Windows 11 permettono di eseguire distribuzioni Linux in finestra e caricare i programmi del pinguino anche con interfaccia grafica. Tutto ciò è possibile grazie alla virtualizzazione e a WSL (Windows Subsystem for Linux).

Supponendo di aver installato, ad esempio, Ubuntu in Windows 10 o in Windows 11, dalla finestra di WSL è possibile accedere al file system del sistema operativo Microsoft sottostante.

Per procedere in tal senso, si può digitare quanto segue per passare alla cartella sulla quale si desidera lavorare:

cd /mnt/d/Documenti

In questo caso, ci si sposta nella cartella D:\Documenti (è possibile effettuare le opportune sostituzioni nel comando precedente).

Comando grep Windows con WSL

A questo punto è poi possibile usare la sintassi del comando grep così come spiegato in apertura.

Credit immagine in apertura: iStock.com – anyaberkut

Ti consigliamo anche

Link copiato negli appunti