Score:-4

Can I make my own custom grep search tool?

in flag

I was trying to find a particular device on my machine. So I typed in lspci. This gave me a large list of items. Too large to constantly look through to see if my device is still attached or not. So I ran:

lspci | grep nvidia

This returned nothing. So I tried "Nvidia" and I got nothing and then finally I tried "NVIDIA" and that's when I got a hit.

Grep seems like an awesome tool however it has some obvious limitations. Is it possible to write my own search type function to accomplish this task and include all cases in the string?

If so, would anyone be able to point me in the right direction on learning how to do this. Even if it's some literature I can read on the topic it would be great.

Even if it takes me a month I'd love to learn how to develop my own tool with that functionality.

Thanks.

TomE avatar
br flag
Try doing a "man grep" in the terminal to get usage instructions. Grep is a very powerful tool aready. For instance, to do what you are trying to do just add the -i option to make it case insensitive like this: lspci |grep -i nvidia
pLumo avatar
in flag
Very bad performance compared to `grep`, but this works: `grepi(){ shopt -s nocasematch; while IFS= read line; do [[ "$line" =~ $1 ]] && printf '%s\n' "$line"; done;}` --> `lspci | grepi nvidia`. However, imo, this is a programming question and thus off-topic here.
Score:6
cn flag

man grep will give you information about how to use grep. The -i option will do what you were looking for: ignore case. Thus, a command

lspci | grep -i nvidia

would have retrieved NVIDIA.

If you want to make your own tool, an easy way is to define an alias:

alias 'grep=grep -i'

This customized grep command will imply the -i option each time you run grep. After defining the alias, your original command would have worked the way you wanted. No need to further program your own tool.

This alias definition is valid for the current session only. To enable the option permanently, open your ~/.bashrc file with a text editor and add the -i option in the alias definition that is there already:

alias grep='grep -i --color=auto'
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.