1

I want to grep the complete word with dot.

Like I have file test.txt

Data in file:

Abc  ABC.xyz abc ABC.xyz 
Def  DEF.mno def DEF.mno

When I am using grep -o I am getting below output

grep -o "\w*ABC\w*" test.txt

Output : ABC

I want complete word ABC.xyz

  • 3
    have you tried ABC\.xyz? – ctrl-alt-delor Apr 20 at 11:20
  • Why are you using grep -o when you want the whole line? Why not just grep ABC test.txt? – Kusalananda Apr 20 at 11:31
  • grep -o is still a good way of getting just the matching portion without the whole line. Is there other data on each line that you don't want? If not, omit the -o option. – Jeff H. Apr 20 at 11:38
  • Apologies!.....I have update the data in file – Arnav Apr 20 at 11:40
  • Right, so: grep -o 'ABC.xyz' test.txt should give you what you need. – Jeff H. Apr 20 at 11:43
1

The problem is that \w means "word characters" ([a-zA-Z0-9_]) and . isn't ever a part of a word:

 $ echo . | grep '\w'
 $ 

You could just search for non-whitespace:

$ echo ABC.xyz | grep '\S*ABC\S*'
ABC.xyz

Note that \S might not be universally understood. If it doesn't work in your implementation, use grep -E instead.

  • Did you try the [^\b] solution on the given input? Probably not what the OP wanted. The \S solution is likely the desired one. – Philippos Apr 23 at 6:09
  • @Philippos d'oh! Ouch, you're quite right. Thanks! – terdon Apr 23 at 8:05
0
awk '/ABC/ { print $NF }' test.txt

This would use awk to match the string ABC against the contents of each line. If a line matches, the last whitespace-delimited word is outputted.

If you want to match against the first field on the line, use $1 ~ /Abc/ rather than /ABC/. If you want to do a string comparison with the first field, use $1 == "Abc".

To pass the string that you compare against on the command line, use e.g.

awk -v str="Abc" '$1 == str { print $NF }' test.txt

To pass a pattern that you compare against on the command line, use e.g.

awk -v pat="ABC" '$1 ~ pat { print $NF }' test.txt

Change $1 to $0 to compare against the whole line.


Using your own approach with grep, and assuming you don't know the filename suffix (if that's what it is), using GNU grep:

grep -o '\<ABC\.\w*\>' test.txt

The \< matches the start of a word while \> matches at the end.

This would give you two ABC.txt though, as it occurs twice on the first line.

With a non-GNU implementation of grep you would use [_[:alnum:]] in place of \w (this would work in GNU grep too).

  • Actually what I was trying to do is that I have to find the word with some pattern. And it is not necessary that word has "." in it but it is possible. So I want to grep the word till next blank space (with dot in result, if present) – Arnav Apr 20 at 15:07
  • @Arnav Added something that will compare a pattern against the first word on the line. Change $1 to $0 to compare against the whole line, or to some other column number to compare against that column. – Kusalananda Apr 20 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.