Grep is a terrific tool to have at your disposal. RELATED: How to Use Pipes on Linux grep: Less a Command, More of an Ally We get a sorted listing of all the files modified in August (regardless of year), in ascending order of file size. sort +4n: Sort the output from grep on the fourth column (filesize).Note that this would also find files that have “Aug” in their names. grep “Aug”: Select the lines from the ls listing that have “Aug” in them.ls -l: Perform a long format listing of the files using ls.We’re listing the files in the current directory, selecting those with the string “Aug” in them, and sorting them by file size: ls -l | grep "Aug" | sort +4n With the next command, we’re piping the output from ls into grep and piping the output from grep into sort. (We could achieve this using the grep -c (count) option, but this is a neat way to demonstrate piping out of grep.) grep "ExtractParameters" *.c | wc -l If we pipe the output from grep into wc and use the -l (lines) option, we can count the number of lines in the source code files that contain “ExtractParameters”. This lets you page through the file listing and to use less's search facility. We know there’s going to be quite a few, so we pipe the output into less: grep "ExtractParameters" *.c | less Let’s say we want to see all occurrences of the string “ExtractParameters” in our C source code files. The file names are listed, not the matching lines. To find out which C source code files contain references to the sl.h header file, use this command: grep -l "sl.h" *.c To see the names of the files that contain the search term, use the -l (files with match) option. grep -B 3 -x "20-Jan-06 15:24:35" geek-1.logĪnd to include lines from before and after the matching line use the -C (context) option. To see some lines from before the matching line, use the -B (context before) option. We’re asking for three lines in this example: grep -A 3 -x "20-Jan-06 15:24:35" geek-1.log To show some lines after the matching line, use the -A (after context) option. it can help distinguish which of the matched lines are the ones you are interested in. We’re going to limit the output to five matching lines: grep -m5 -n Jan geek-1.logīeing able to see some additional lines-possibly non-matching lines-for each matching line is often useful. To reduce the number of results that are displayed, use the -m (max count) option. The line number for each matching line is displayed at the start of the line. Comments are great, but sometimes it’s hard to spot the actual settings in amongst them all. This can be useful when you’re looking at configuration files. The opposite of that is only showing the lines that don’t match. The single line that matches is found and displayed. Let’s search for a date and time stamp that we know appears only once in the log file: grep -x "20-Jan-06 15:24:35" geek-1.log The -x (line regexp) will only match lines where the entire line matches the search term. It tells grep to match any one of the characters contained within the brackets “.” This means grep will match either “kB” or “KB” as it searches.īoth strings are matched, and, in fact, some lines contain both strings. We’re making use of the regular expression bracket feature to create a search pattern. The -e (patterns) option allows you to use multiple search terms on the command line. You can also search for multiple terms that are not necessarily whole words, but they can be whole words too. We’re going to search for the word “free.” grep -i free geek-1.logĪll of the matching lines are displayed for each of the search terms. The symbolic link is followed and the directory it points to is searched by grep too.īy default, grep will match a line if the search target appears anywhere in that line, including inside another string. Let’s repeat our last search with the -R (recursive dereference) option: grep -R -i memfree. We’ve got a symbolic link in this directory, called logs-folder. We can make grep follow symbolic links by using the -R (recursive dereference) option. The output includes the directory and filename of each matching line. Here we’re searching in the current directory “.” and any subdirectories: grep -r -i memfree. Note that you don’t provide a file name on the command line, you must provide a path. To search through nested directories and subdirectories, use the -r (recursive) option. We can check the return code using the $? special parameters: grep -q average geek-1.log echo $? grep -q howtogeek geek-1.log echo $? A result of zero means the string was found, and a result of one means it was not found. The result is passed to the shell as a return value from grep. We can cause grep to be completely silent.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |