![]() You can see in the image below that along with the line numbers, the name of the file matching the pattern is also printed. I will use the following command for this. It would mean that grep must search in all files in that directory. Now if I wanted to search for a pattern or expression in all those files I would just put a * instead of the filename. It is in the same directory as my first file. I hope you have now enough knowledge of grep command and how it is used in various conditions.Now I have created another file called LNU.txt which has the following lines of text as follows. In the above tutorial, you learned how to use the grep command to search for a specific string in files. You can print only matching patterns using the -o option.įor example, search for file1.txt that matches the string/pattern linux with the following command: grep -o linux file1.txt You can also display one line before and after the matching string using the option c and n with grep command.įor example, display one line before and after the matching string linux in file4.txt, run the following command: grep -n -C 1 linux file4.txtīy default, grep command prints the entire line which matches a pattern. You can use grep with -n option to print line numbers with matching patterns.įor example, to display line number that matches the pattern linux in the current directory, run the following command: grep -n linux *įile1.txt:1:tecadmin is a popular linux blogįile2.txt:2:linux is made by linus torvalds.įile2.txt:3:linux is most popular operating system.įile3.txt:2:Ubuntu is a linux operating system You can use grep with -c option to display all files with the number of lines that matches the given string.įor example, to display all files with the number of lines that matches a string linux in the current directory, run the following command: grep -c linux *ĭisplay Line Number with Matching Pattern You can display only filenames that contain a specific string using -l option.įor example, list all filenames in the current directory that matches the string tecadmin, run the following command: grep -l tecadmin * To List Filenames That Matches Specific Pattern This command will exclude all the lines that contain the string linux: You can use grep command with -v option to print all lines that do not match a specific pattern of characters.įor example, print all lines that don’t contain the string linux in file1.txt and file2.txt, run the following command: grep -v linux file1.txt file2.txt This command will print all lines that contain a word linux in all files in the current directory and sub-directories:įile3.txt:Ubuntu is a linux operating system
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |