0

Its been asked several times but its not clear to me yet.

I have the following text in a file ( data.txt, tab delimeted ):

ABC 12 ABC-AS 14 DEF 18 DEF-AS 9 

Now I want to search for ABC and DEF, but not ABC-AS, DEF-AS as a result.

grep -w ABC data.txt 

Output:

grep -w ABC data.txt ABC ABC-AS grep --no-group-separator -w "ABC" data.txt ABC ABC-AS grep --group-separator="\t" -w "ABC" data.txt ABC ABC-AS 
3
  • If your version of grep has Perl regexes available, you can use a negative lookahead. You can look for ABC and then post-filter grep -v 'ABC-[A-Z]' or thereabouts. Commented Sep 28, 2017 at 15:59
  • 1
    With the revised description, can you use grep '^ABC[[:space:]]' to find the lines that start with ABC and not ABC-AS? Or grep -E '^(ABC|DEF)[[:space:]]' if you want DEF too. Commented Sep 28, 2017 at 16:06
  • 1
    Or grep "ABC\s" data.txt. For multiple patterns: grep -e "ABC\s" -e "DEF\s" data.txt Commented Sep 28, 2017 at 16:11

2 Answers 2

1

With a regex

grep -E "(ABC|DEF)[^\-]" data.txt 

Details

(ABC|DEF): Match "ABC" or "DEF"

[^\-]: Anything except "-"

Output

ABC 12 DEF 18 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this, which select only those matches that exactly match the whole line

grep --line-regexp "ABC" data.txt 

2 Comments

I am sorry. the file contains other columns. I updated the query to reflect exact situation.
In this case, try this: grep "ABC[[:blank:]]" .\data.txt

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.