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

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.

Related

Capturing string between 2 specific letters/words using shell scripting

I am trying to capture the string between 2 specific letters/words using sed/awk. This is what I am trying to do:
The input is a file test.log containing
Owner: CN=abc.samplecerrt.com,o=IN,DC=com
Owner: CN=abc1.samplecerrt.com,o=IN,DC=com
I want to extract only "CN=abc.samplecerrt.com"
I tried
sed 's/.*CN=\(.*\),.*/\1/p' test.log >> result.log
But this returns "abc.samplecerrt.com,o=IN,DC=com"
How do I go about this?
test file:
$ cat logs.txt
CN=abc.samplecerrt.com,o=IN,DC=com Owner: CN=abc1.samplecerrt.com,o=IN,DC=com
command and output:
$ grep -oP 'CN=(?:(?!CN=).)*?.com' logs.txt
CN=abc.samplecerrt.com
CN=abc1.samplecerrt.com
This might work for you (GNU sed):
sed -n 's/.*\(CN=[^,]*\).*/\1/p' file
Or:
sed 's/.*\(CN=[^,]*\).*/\1/p;d' file
The first turns off implicit printing -n so as to act like grep.
Matches and captures the string CN= followed by zero or more non-comma characters and prints the captured group \1 if a match is made.
The second solution is much the same except it deletes all lines and only prints the captured group as above.
With awk you can get the field where is the string you need. For it, you can set FS=:|, Now if you run
awk -v FS=":|," '{print $2}' file
CN=abc.samplecerrt.com
CN=abc1.samplecerrt.com
you get the field. But you only want one, so
awk -v FS=":|," '$2 !~ /abc1/ {print $2}' file
CN=abc.samplecerrt.com

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$'

Grep substrings in string/word

Is there a way on grep or any other unix tool to search for a sequence of substrings in a string?
To clarify:
$ grep "substring1.*subrstring2"
substring1_mySubstring2 # OK substrings forming a single string
substring1 substring2 # WRONG substrings are separated`
You can tell grep to look for substring1 + some characters + substring2:
grep -iE 'substring1\w+substring2' file
Note the usage of -i to ignore case and -E for an extended regex coverage (the same without -E could be done with \w\+ instead).
Test
$ cat a
substring1_mySubstring2
substring1 substring2
substring1_and_other_things12345substring2 blabla
Let's see how this matches just when there is no spaces in between:
$ grep -iE 'substring1\w+substring2' a
substring1_mySubstring2
substring1_and_other_things12345substring2 blabla

Grep return wrong result

I need search lines without term \t42\t.
I use:
grep -w -v '\t42\t' file.txt > tmp.txt
Why have I line with term \t42\t in result file?
You're getting this result because for grep the \t sequence means "one tab character". You must escape the backslash characters for them to be treated litteraly:
grep -w -v '\\t42\\t' file.txt > tmp.txt
Remove -w as it doesn't work if you append non-word characters to the pattern:
grep -v '\t42\t' file.txt > tmp.txt

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'

Resources