grep exclude multiple strings - linux

I am trying to see a log file using tail -f and want to exclude all lines containing the following strings:
Nopaging the limit is and keyword to remove is
I am able to exclude one string like this:
tail -f admin.log|grep -v "Nopaging the limit is"
But how do I exclude lines containing either of string1 or string2?

Filtering out multiple lines with grep:
Put these lines in filename.txt to test:
abc
def
ghi
jkl
grep command using -E flag with a pipe between tokens in a string:
grep -Ev 'def|jkl' filename.txt
prints:
abc
ghi
egrep using -v flag with pipe between tokens surrounded by parens:
egrep -v '(def|jkl)' filename.txt
prints:
abc
ghi
Or if stacking -e flags through grep parameters is okay (credit -> #Frizlab):
grep -Fv -e def -e jkl filename.txt
prints:
abc
ghi

grep -Fv -e 'Nopaging the limit is' -e 'keyword to remove is'
-F matches by literal strings (instead of regex)
-v inverts the match
-e allows for multiple search patterns (all literal and inverted)

Another option is to create a exclude list, this is particulary usefull when you have a long list of things to exclude.
vi /root/scripts/exclude_list.txt
Now add what you would like to exclude
Nopaging the limit is
keyword to remove is
Now use grep to remove lines from your file log file and view information not excluded.
grep -v -f /root/scripts/exclude_list.txt /var/log/admin.log

egrep -v "Nopaging the limit is|keyword to remove is"

tail -f admin.log|grep -v -E '(Nopaging the limit is|keyword to remove is)'

You can use regular grep like this:
tail -f admin.log | grep -v "Nopaging the limit is\|keyword to remove is"

The greps can be chained. For example:
tail -f admin.log | grep -v "Nopaging the limit is" | grep -v "keyword to remove is"

If you want to use regex:
grep -Ev -e "^1" -e '^lt' -e 'John'

Related

How to search exact phrase from a file which consist of set of phrase with hyphen

I have the file, which consists of a couple of phrases as follows. I would like to grep the exact match from out of them.
file.txt
abc
abc-def
xyz
xyz-pqr
pqrs
If I search "abc" I need to return only abc.
or
if I search "abc-def" i need to return only "abc-def"
preferd output
$grep -w "abc" file.txt
abc
or
$grep -w "abc-def" file.txt
abc-def
the below method is not working for the hyphens
$grep -w abc file.txt
With your given data/file you can use the -x flag.
grep -x abc file.txt
grep -x abc-def file.txt
-x, --line-regexp force PATTERN to match only whole lines
The -x flag is defined/required by POSIX grep(1)
In order to match an entire line you need to match the start and end of the line:
grep '^abc$' file.txt
grep '^abc-def$' file.txt
You can use awk this way:
awk -v w="abc" '$1==w' file.txt
abc
Or,
awk '$1==w' w="abc" file.txt
With the == operator, it only returns exact string matches. We are setting what to match with w="abc" either with the -v switch or through stdin.

How to loop variable values to ignore them in a csv file unix?

I have this content in file.csv
cat file.csv
QUOTA,landscape=test,region=europe,limit=N2_CPUS quota=24.0,quota_used=0.0,quota_used_percent=0
QUOTA,landscape=test,region=europe,limit=COMMITTED_N2_CPUS quota=0.0,quota_used=0.0,quota_used_percent=0
QUOTA,landscape=test,region=europe,limit=COMMITTED_C2_CPUS quota=0.0,quota_used=0.0,quota_used_percent=0
QUOTA,landscape=test,region=europe,limit=RESERVATIONS quota=100.0,quota_used=0.0,quota_used_percent=0
I need to remove values which contain strings "RESERVATIONS" and "N2_CPUS" and the variables can be random
variable=("RESERVATIONS","N2_CPUS")
I am able to do when i use one value as variable using
cat file.csv | grep -v $variable
When there are more values in a variable, even loops are not working as expected. Could you please suggest?
I would use egrep (or grep -E, depending on your flavor of linux)
variable="RESERVATIONS|N2_CPUS"
cat file.csv | egrep -v $variable
or
cat file.csv | grep -Ev $variable
Note, though, in your example, the cat is not required:
grep -Ev "${variable}" file.csv
Notice the quotes around the variable, you may need those as well, depending on your shell & Linux version.
egrep (or grep -E) is an grep with Extended Regular Expression. The vertical bar, or pipe | separates the values. Effectively it is saying OR. Thus,
egrep -Ev "A|B" means look for 'A' or 'B' and remove them.
Use grep -E so you can use an extended regular expression, and then use | in the regexp to match multiple strings.
variable=RESERVATIONS|N2_CPUS
grep -v -E "$variable" file.csv

Search files for a string but exclude another string

I want to search files for \xc2 but exclude \xc2\xbb
I have grep -rnwl '/home/rascalofthenorth/development/html/' -e "xc2"
You can also do it using -v option of grep like this:
grep -rnwl '/home/rascalofthenorth/development/html/' -e "xc2" | grep -v "xbb"
The -v option shows only results not containing xbb.
Or for more specific cases in which you want, say, xc2 to appear and xbb not to appear, then awk comes in handy:
awk '/xc2/ && !/xbb/' <yourfile>

Why grep gives error for search pattern i.e. grep: <some pattern>: No such file or directory

I want to print the selected lines using grep pattern matching. I am using following command -
cat MyTest.txt | grep -v -E B1 "EEB|SET|PET"
grep: EEB|SET|PET: No such file or directory
I am always getting above grep error.
I want to print the line which matches pattern or patterns I have mentioned i.e EEB or SET or PET or All of these and
A single line prior to matching line. hence option -B1
You can use this command without useless cat:
grep -v -E -B1 "EEB|SET|PET" MyTest.txt
Note - before B1.
However from your description it appears you may not need -v (inverse results) and want this:
grep -E -B1 "EEB|SET|PET" MyTest.txt
Grep has the following syntax:
grep options pattern input_file_names
where options are optional, but not pattern. So B1 without "-" is used as pattern and "EEB|SET|PET" as file_names.
You should change "B1" to "-B1".
As recommendation
cat MyTest.txt | grep -v -E -B1 "EEB|SET|PET"
to
grep -v -E -B1 "EEB|SET|PET" MyTest.txt

Linux grep command

Can I use grep command to look for all the lines in a file that have"abc" in them, but exclude the lines that end in say "xyz"?
Eg grep 'abc' fileName (some way to exclude all lines ending in "xyz")
Try this:
hzhang#dell-work ~ $ cat sample.csv
abc, xyz
abc,1
abc,2
abc,3,xyz
hzhang#dell-work ~ $ grep abc sample.csv |grep -v "xyz$"
abc,1
abc,2
The explanation of -v:
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
If you can use awk, just check the patterns:
hzhang#dell-work ~ $ awk '/abc/ && !/xyz$/' sample.csv
abc,1
abc,2
awk fit's pretty good for such cases:
awk '/abc/ && !/xyz$/' input
use awk! :)
Use two grep commands piped together. The first matches abc, the second removes the ones that end with xyz.
grep abc filename | grep -v 'xyz$'

Resources