How to make grep only match if the entire line matches?

man grep
grep regex
grep exact match whole word
grep print only the matched parts instead of whole matching lines
grep options
grep print only match regex
grep print whole line
grep print line containing text

I have these:

$ cat a.tmp

I wanted to find a exact match of ABB.log.

But when I did

$ grep -w ABB.log a.tmp

it shows all of them.

Can I get what I wanted using grep?

Simply specify the regexp anchors.

grep '^ABB\.log$' a.tmp

How to use grep command in UNIX / Linux {With Examples}, Which option with the grep command displays only the number of matching lines in a file? quoting the string and escaping the dot (.) makes it to not need the -F flag any more. You need to escape the. (dot) (because it matches any character (not only.) if not escaped) or use the -F flag with grep. -F flag makes it a fixed string (not a regex). If you don't quote the string, you may need double backslash to escape the dot (.

grep -Fx ABB.log a.tmp

From the grep man page:

-F, --fixed-strings Interpret PATTERN as a (list of) fixed strings -x, --line-regexp Select only those matches that exactly match the whole line.

grep Command In Linux / UNIX with complete examples, 'pattern1\|pattern2' file Always enclose the regular expression in single quotes to avoid the interpretation and expansion of the meta-characters by the shell. explanation of each grep option:-o, --only-matching, print only the matched part of the line (instead of the entire line) -a, --text, process a binary file as if it were text -m 1, --max-count, stop reading a file after 1 matching line -h, --no-filename, suppress the prefixing of file names on output -r, --recursive, read all files under a directory recursively

Here is what I do, though using anchors is the best way:

grep -w "ABB.log " a.tmp

How to Grep for Multiple Strings and Patterns, If I am not mistaken, grep shows the whole line for which a match has been found​. For a specific word, I use grep directly instead of cat | grep . grep -w "foobar"  However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could. I'd really like to cat a file and see the entire file with the pattern matches highlighted.

Most suggestions will fail if there so much as a single leading or trailing space, which would matter if the file is being edited by hand. This would make it less susceptible in that case:

grep '^[[:blank:]]*ABB\.log[[:blank:]]*$' a.tmp

A simple while-read loop in shell would do this implicitly:

while read file
  case $file in
    (ABB.log) printf "%s\n" "$file"
done < a.tmp

How to make grep command return entire matching line, but I only need this: Note: option -x matches the whole line. Try one of: grep -​w "deiauk" textfile grep "\<deiauk\>" textfile Try this with GNU grep and mark word boundaries with \b : grep "\bdeiauk\b" file. Output: If your grep supports -P (PCRE), you can do: If you know it has to be at the start of the line, check for it: grep(1) has a -o flag that outputs only the matching part of the line. From the man page:-o, --only-matching Show only the part of a matching line that matches PATTERN. Your pattern isn't right to get the output you want, though. Try: $ egrep -o 'key \w+' file key word1 key word2

similarly with awk

 awk '/^ABB\.log$/' file

Match exact string using grep, grep searches the named input FILEs (or standard input if no files are named, or if a -x, --line-regexp: Select only those matches that exactly match the whole line. of each input file from which no output would normally have been printed. Use \b to match on "word boundaries", which will make your search match on whole words only.. So your grep would look something like. grep -r "\bSTRING\b" adding color and line numbers might help too

grep(1): print lines matching pattern, If you want only the part inside the parensis to be matched: 1/p" -n don't print lines s substitute ^. I don't think grep or egrep can do this, perl and sed can. As stated by @Rory, you need the -o option, so only the match are printed (instead of whole line) In addition, you neet the -P option, to use Perl regular expressions, which include useful elements like Look ahead (?=) and Look behind (?<=), those look for parts, but don't actually match and print them.

Don't need the whole line, just the match from regular expression , The only line that contains a match is line 1 of argmatch.h . written without the square brackets, it would have matched not only the ps output line for If grep listed all matching “lines” from a binary file, it would probably generate output that​  The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. grep -x “phoenix number3” * The output shows only the lines with the exact match.

Usage (GNU Grep 3.4), Another common way to use grep is with a pipe, making it a sort of filter. grep --​only-matching --line-number Fedora example.txt 2:Fedora To search all files in all subdirectories of a specific folder, use --recursive or -r : -w: Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. cat *-text-file | grep -Eio "th [a-z]+"

  • Both anchors (^ and $) are needed.
  • Nice one! And if I'm using the regex for the matching from a file? "grep -f patterns a.tmp" ??
  • @green69 Several years late, but you can use sed to add the anchors before passing the patterns to grep: sed -r "s/^(.*)$/^\1$/" patterns.txt | egrep -f - a.tmp
  • wasn't able to use -F unfortunately.
  • @Johnyy No -F ? are you on Solaris? If so use /usr/xpg4/bin/grep
  • I'm using grep inside a bash script and this option is better than use a regular expression as suggested in the accepted answer. Because I have some special character inside the variable I'm searching (like .) and I don't have to escape them when using the command.
  • best answer IMO because it allows special characters without escaping
  • This is better because it also works with variables.
  • Like this one.. it will return First line match
  • This requires a space after ABB.log, which is not the general case, i.e it will fail most of the time.
  • Did you actually experience that -F was missing in your case? If so, can you please say which case that was?
  • @hakre Did you read my answer completely? I explained (pretty clearly) why -F won't be needed if the . is escaped properly.
  • Sure. Sure, I'm just asking: Was -F available when you wrote this? If not, which system were you using? I couldn't find that information in your answer so far, which even now is the case when I re-read it again. So really sure, I read your answer completely at least twice. ;)
  • @hakre Of course -F was available. ( I did say: "or use -F flag with grep")