- Grep with the Line Number in Output
- Prerequisite
- The Line Number for Matching a Word
- The Line Number of the Whole Text in the File
- Show Only Line Number
- Provide Output in a Single Line
- Show Line Number of the String within the Subdirectory
- Show according to a word in the file
- Bashrc
- Search in all Files
- Search in Files Extensions
- Conclusion
- About the author
- Aqsa Yasin
- How to get line number from grep?
- 4 Answers 4
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- How to display line number while doing grep on a file
- You must log in to answer this question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Show filename and line number in grep output
- 5 Answers 5
Grep with the Line Number in Output
Global regular expression print is a versatile utility that searches plain text in the system with different regular expressions. We can perform many operations with the help of Grep; we can explore in files, display line number as output, and how to ignore blank spaces, and use Grep recursively. Grep with the line number displays the line number of relevant text present in the file. This function is accomplished with the help of –n. From the page of Grep, we can easily describe different commands.
Prerequisite
To achieve this current goal of obtaining a specific line number of the text, we must have a system to run commands on it which is the Linux operating system. Linux is installed and configured on the virtual machine. After providing a username and password, you will be able to access the applications.
The Line Number for Matching a Word
Generically when we use the Grep command, after the Grep keyword, the word that has to be explored is written and followed by the filename. But, by getting the line number, we will add -n in our command.
Here “is” is the word that is to be explored. The starting line number shows that the related file contains the word in different lines; each line has a highlighted word that shows the matching line to the relevant search.
The Line Number of the Whole Text in the File
The line number of every line in the file has shown by using a particular command. It not only shows the text but also covers the blank spaces and mentions their line numbers too. The numbers are shown on the left side of the output.
Fileb.txt is a filename. Whereas n is for the line numbers, and l shows the filename only. In case we have searched a specific word in any file, it will only show the filenames.
Concurrent to the previous example, here are (except for free space), which are special characters that are mentioned. They are also shown and read by the command to display the line number. Unlike the first example of the article, this simple command shows the line’s number exactly how it is present in the file. As there is no limitation of search declares in command.
Show Only Line Number
To get only the line numbers of data in the respective file, we can easily follow the below command.
The first half command before the operator is understandable because we have discussed earlier in this article. Cut –d is used to cut the command, which means to suppress the display of text in the files.
Provide Output in a Single Line
Following the command above, the output is display on a single line. It removes the extra space between the two lines and only shows the line number mentioned in the previous commands.
The right portion of the command shows that how the output is displayed. The cut is used to cut the command. Whereas second “|” is applied for bringing to the same line.
Show Line Number of the String within the Subdirectory
In order to demonstrate the example on subdirectories, this command is used. It will search for the word “1000” present in files in this given directory. The file number is shown at the starting of the line on the left side of the output, showing the occurrence of 1000 in the prcd folder at 370 ties and in Webmin is 393 times.
This example is good in finding an error occurring chances in your system by checking and sorting particular words from the directory or subdirectory. The /etc/ describes the path of the directory having a folder of services.
Show according to a word in the file
As already described in the examples above, the word helps search the text inside the files or folder. Searched words will be written in inverted commas. On the very left side of the output, a line number is mentioned, showing the occurrence of the name on which line in a file. “6” shows that the word Aqsa is present on line 6 after line 3. Highlighting the specific word makes it easier for the user to understand this concept.
The output shows the whole string in the file, not only the single word present in the string, and it only highlights the given word.
Bashrc
This is a useful example of getting the line number in the output. This will search in all directories, and we don’t have to provide the directory path. By default, it is implemented on all directories. It shows all the output data on the files present in subdirectories, as we don’t have to mention a specific word to be searched through the command.
It is an extension of all folders that are present. By specifying the extension name, we can show the relevant data, i.e., login detailed files.
Search in all Files
This command is used in searching the file in all files having that data. File* shows that it will search from all files. The filename is displayed with the line number after the name at starting of the line. The relevant word is highlighted to show the existence of the word in the text in the file.
Search in Files Extensions
In this example, the word is searched in all files of a specific extension, that is.txt. The Directory that is given in the command is the path of all files provided. The output also shows the way according to the extension. The line number is given after the filenames.
Conclusion
In this article, we have learned how to obtain the line number in the output by applying different commands. We hope this effort will help in gaining enough information regarding the relevant topic.
About the author
Aqsa Yasin
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.
How to get line number from grep?
I would like to know what line has the following text, I tried adding -n , but it did not work. I tried adding | grep -n * , but it did something weird. What I would like to see (I don’t care about format) is
4 Answers 4
get line number of a pattern!
if you want to get only the line number as output add another grep command to it !
grep -n "pattern" file.txt | grep -Eo '^[^:]+'
You should put -e at the end of the options list: grep -rne coll *
To grep a pattern in a specific file, and get the matching lines:
or using cut as suggested by @wjandrea:
grep -n | cut -f1 -d: | sort -u
- is a quoted glob pattern (use option -E for regexp);
- is the file you are interested in;
- the first pipe awk . filters the line numbers in the output of grep (before : on each line);
- the second pipe ensures that line numbers only appear once.
#This shows only the line number. #i --> case-insensitive, n --> line number var="APPLE";targetFile="targetFile.txt" numLin=$(grep -in $var $targetFile | cut -d':' -f1); echo $numLine;
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to display line number while doing grep on a file
There is the option -n , and many more in the manual page, worth reading.
It is probably worth noting that using -n with -l does not work. -l will take precedence and only the name of the file will be printed.
grep -n prefixes each line of output with the line number in the input file. Is this what you are looking for?
alias grep=’grep -inE —color=auto’
This will color the match as well for easy reading.
Have you checked cat -n ‘filename’ . This will print the line numbers.
cat -n /boot/config | grep CONFIG_PM_ADVANCED_DEBUG
cat will -n[umber] the lines and | (filter) through grep looking only for lines with CONFIG_PM. in them
the resulting output will be line number; the text of the line and the string
cat -n[umber lines] /Path/to/filename | grep -i[gnor case (optional)] STRING_TO_LOOK_FOR
Welcome to U&L. I edited your post, so it utilizes the formatting possibilities of the site a bit more. Please review the changes (with the edit history), so your future answers (and questions) might look better. It is also not necessary to «sign» your post, your name is posted with your profile on all answers
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Show filename and line number in grep output
I am trying to search my rails directory using grep. I am looking for a specific word and I want to grep to print out the file name and line number. Is there a grep flag that will do this for me? I have been trying to use a combination of -n and -l but these are either printing out the file names with no numbers or just dumping out a lot of text to the terminal which can’t be easily read. ex:
5 Answers 5
I think -l is too restrictive as it suppresses the output of -n . I would suggest -H ( —with-filename ): Print the filename for each match.
If that gives too much output, try -o to only print the part that matches.
This answer worked out also for me. I was in need of searching recursively and used this example command: grep -Hnor «localhost» . This listet up all matches with file name and line number, short and concise.
grep -rin searchstring * | cut -d: -f1-2
This would say, search recursively (for the string searchstring in this example), ignoring case, and display line numbers. The output from that grep will look something like:
/path/to/result/file.name:100: Line in file where 'searchstring' is found.
Next we pipe that result to the cut command using colon : as our field delimiter and displaying fields 1 through 2.
When I don’t need the line numbers I often use -f1 (just the filename and path), and then pipe the output to uniq , so that I only see each filename once:
grep -ir searchstring * | cut -d: -f1 | uniq