Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linux cmd line: How to filter a string by NOT wanted patterns

On Debian Linux:

I’m wanting to do a search and then filter it by those lines that do NOT begin with "/mnt/c/" or "/mnt/x/", my current disks (I’m working on WSL).

So then I write: `locate $str | grep /[!cx]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I learned a bit about wildcards but I don’t know what I’m doing wrong. Any help would be appreciated.

>Solution :

You need to use grep regexes rather than shell wildcards:

locate "$str" | grep -v '^/mnt/[cx]/'

Put quotes around $variable as a matter of routine — there are occasions not to do so, but they are rare.

The -v option to grep means "print the lines that don’t match". The regex '^/mnt/[cx]/' means lines that start with /mnt/c/ or /mnt/x/, so in conjunction with -v that means "only print lines that don’t start with /mnt/c/ or /mnt/x/.

I’m blithely assuming that the locate command provides the data you want to filter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading