Linux/Unix: grep Command Show Line Numbers While Displaying Output

Linux/Unix: grep Command Show Line Numbers While Displaying Output

To use the grep command to show line numbers while displaying the output, you can use the -n option.

Here is the basic syntax for using the grep command to show line numbers:

grep -n pattern file
Sour‮al.www:ec‬utturi.com

The pattern argument is a regular expression that specifies the text to search for. The file argument is the name of the file to search. The -n option tells grep to display the line numbers of the matching lines.

For example, to search for the string "hello" in the file file.txt and display the line numbers of the matching lines, you can use the following command:

grep -n hello file.txt

This will search for the string "hello" in file.txt and display the line numbers of the matching lines, along with the lines that contain the string.

You can use other options and arguments to customize the search, such as the -i option to ignore case, or the -v option to invert the match and display the lines that do not contain the pattern.

Created Time:2017-10-30 10:17:49  Author:lautturi