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

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

Related

How to extract a single word from the output of a Linux command?

$ lsusb --verbose | grep "THRSL_C_C_V"
results in:
iManufacturer 1 THRSL_C_C_V3.07
I want to extract the word THRSL_C_C_V3.07 only.
I tried lsusb --verbose | grep -w "THRSL_C_C_V". Didn't show anything.
Try:
lsusb --verbose | grep -o "THRSL_C_C_V3\.07"
The -o options results in only matching text, the backlash escapes the dot allowing an exact match of the requested text.
You may use awk instead of grep:
lsusb --verbose | awk '/THRSL_C_C_V/{print $3}'
This awk command searches a line that has text THRSL_C_C_V in it and by using print $3 we make sure to print 3rd column of matched line.
Alternatively you can use grep -o like this:
echo 'iManufacturer 1 THRSL_C_C_V3.07' |
grep -oE 'THRSL_C_C_V[^[:blank:]]*'
THRSL_C_C_V3.07
Use the following:-
$ lsusb --verbose | grep "THRSL_C_C_V" | awk '{print $NF}'
or
$ lsusb --verbose | awk '/THRSL_C_C_V/{print $NF}'
Here, $NF will be used for the last element of output

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

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'

How to select not commented rows?

I have file that contains:
# sdfdsfds fsf
var1=1232
#fdsfdsfds
#fdsfsdf
var2=456
..................
I need select only not commented rows - that not start from #
Does it possible with grep?
Thanks.
use the -v(invert-match) option in grep:
$ grep -v '^#' file.txt
var1=1232
var2=456
The following will do it:
grep -v ^# file.txt
You may use grep -v '^#'
The -v option is for not logic.
Use the -E option for regex and -v option for inverse matching.
grep -v -E '^#' file

Resources