Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep -l and grep -ln

Tags:

grep

unix

according to the manual for grep,

-l, --files-with-matches
          Suppress normal output; instead print the  name  of  each  input
          file  from  which  output would normally have been printed.  The
          scanning will stop on the first match.

grep -l, this seems fine in that when a match is found, the file name containing the match is echoed.

However when i do a grep -ln, grep echoes every line of the occurrence.

Does grep -l really mean to stop when the first occurrence of the match is found and stop scanning, while grep -ln will ignore the -l flag?

like image 395
Oh Chin Boon Avatar asked Aug 04 '11 02:08

Oh Chin Boon


People also ask

What is the difference between grep and?

The main difference between grep and egrep is that grep is a command that allows searching content according to the given regular expression and displaying the matching lines while egrep is a variant of grep that helps to search content by applying extended regular expressions to display the machining lines.

Is grep faster than grep?

Is fast grep faster? The grep utility searches text files for regular expressions, but it can search for ordinary strings since these strings are a special case of regular expressions. However, if your regular expressions are in fact simply text strings, fgrep may be much faster than grep .

Why is grep called grep?

Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect. grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others such as OS-9.

What does grep grep do?

Grep is an acronym that stands for Global Regular Expression Print. Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. The text search pattern is called a regular expression. When it finds a match, it prints the line with the result.


1 Answers

Those options are incompatible. Use grep -Hnm 1 if you want to display the line number of the first match (and only the first match) in each file.

-H, --with-filename
Print the filename for each match.

-n, --line-number
Prefix each line of output with the line number within its input file.

-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.

like image 197
John Kugelman Avatar answered Sep 19 '22 08:09

John Kugelman