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

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

Related

Display the lines of file that contain the pattern 'Pap' but they don't contain the pattern 'Aig'

How to make such a double query ?
I try grep -e Pap -e -v Aig filename.txt and grep 'Pap' && -v 'Aig' filename.txt
but both don't work
Use a pipe between your filters
grep -e Pap filename.txt | grep -v Aig

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.

grep a particular content before a period

I am trying to read/grep a particular word or content that is before a period (.).
e.g. file1 has abinaya.ashok and I want to grep whatever is before the period (.) without hardcoding anything.
if I try
grep \.\ file1
it gives abinaya.ashok.
I've tried: grep\*\.\ file1
it doesn't give anything.Can we find it using grep commands or should we do it only using awk command? Any thoughts?
Using GNU grep for PCRE regex (for non-greedy and positive look-ahead), you can do:
echo 'abinaya.ashok' | grep -oP '.*?(?=\.)'
abinaya
Using awk:
echo 'abinaya.ashok' | awk -F\. '{print $1}'
abinaya
Check the following simple examples.
Including the dot:
$ echo abinaya.ashok | grep -o '.*[.]'
abinaya.
Without the dot:
$ echo abinaya.ashok | grep -o '^[^.]\+'
abinaya
Hope I understand you correctly:
sed -n 's/\..*//p' file1 | grep whatever
sed expression will print only part before dot (lines without dot are not printed).
Now use grep to search what you need.

grep exclude multiple strings

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'

grep with negative pattern

I am looking for a way to grep a file for a specific pattern with negative pattern in it.
I have a log file witch reports units version and I want to see if there is a unit witch report version other then 26.
The closest I could get is :
cat my.log | grep -i -e "version=0x[^2][^6]"
The above return a line contain "version=0x13" but not return a line contain "version=0x23"
Is there a way to tell grep to do so ?
Thanks.
Interpret the pattern as a perl regular expression using the -P switch:
grep -iP 'version=0x(?!26)\d\d' my.log
grep -i "version=0x[0-9]\\+" my.log | grep -iv "version=0x26"

Resources