Networking-Forums.com

General Category => Forum Lobby => THE MUSEUM OF FORUM FAIL => Topic started by: Gabriel9999 on April 03, 2019, 04:31:42 AM

Title: Exclude unwanted string from Linux grep
Post by: Gabriel9999 on April 03, 2019, 04:31:42 AM
I have a configuration file which is a simple text. I want to list some configuration line but except some sitring. How can I use Linux grep to exclude and search for given strings? Thanks in advance for your help!
Title: Re: Exclude unwanted string from Linux grep
Post by: icecream-guy on April 03, 2019, 05:53:13 AM
Have you checked MAN page for grep? there is option for this.
Title: Re: Exclude unwanted string from Linux grep
Post by: Otanx on April 03, 2019, 09:13:26 AM
The lazy man way;

cat file.txt | grep "match this" | grep -v "not this"

I used to have a sticky note with a drawing of a cat that said "gratuitous use of cat award" one of our devs gave me. It disappeared when we moved.

-Otanx
Title: Re: Exclude unwanted string from Linux grep
Post by: Dieselboy on April 04, 2019, 01:19:41 AM
I think egrep does it?

When I want to view a LONG config file that has like 10 lines of config and the rest are comments, I use this (which I stole from someone else)

egrep -v "\#|^$" someconfig.conf

Dont forget you can use regex as well. And dont foget to check the man pages! Usually they have examples there and if you were to do a linux exam this is all that you're allowed to check. So get used to it :)

man grep

man egrep
Title: Re: Exclude unwanted string from Linux grep
Post by: samueltarcin9999 on April 05, 2019, 02:36:27 AM
There are different ways to exclude and grep given a text file or input stream. I generally prefer the most basic one where use two grep where the first one is for exclude with -v option and pipe to second one where I grep for my string.   grep -v 'able' config.txt | grep 'system'  For more information look https://www.yjastar.com/ku/introduction-to-linux-grep-command-with-examples/