Let’s assume we want search ‘IP_Forward’ string in nf file, run $ grep IP_Forward /etc/nf When we use ‘-i’ then it will not discriminate upper case or lower case letters while searching. ‘-i’ option in the grep command ignore case distinctions in patterns and data. To print the line numbers of empty lines, run $ grep -n '^$' /etc/nf 10) Ignore letter case while searching Grep command can also print all the empty or blank lines from a file use the special character combination ‘^$’, example is shown below: $ grep '^$' /etc/nf etc/passwd-:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologinĪbove command will search ‘nobody’ pattern in the “/etc” directory recursively. etc/ssh/sshd_config:#AuthorizedKeysCommandUser nobody Let’s suppose, we want to search a pattern ‘nologin’ in /etc folder recursively. ‘-r’ option in grep command is used to search pattern recursively in folder and sub-folders. Linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash List all the lines of /etc/passwd that ends with “ bash” word. $ 7) Print all the lines that ends with specific wordīash shell treats dollar symbol ‘$’ as a special character which marks the ends of line or word. Let’s display the lines which starts with “backup” word in the file /etc/passwd, run $ grep ^backup /etc/passwdīackup:x:34:34:backup:/var/backups:/usr/sbin/nologin Using the option ‘-v’ in grep command, we can display the lines which don’t match the pattern $ grep -v 'nobody' /etc/passwdĦ) Print all lines that starts with specific patternīash shell treats caret symbol (^) as a special character which marks the beginning of line or a word. In below example, pattern is ‘nobody’ $ grep -n 'nobody' /etc/passwdġ8:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin Use ‘-n’ option in grep command to display line and its number which matches the pattern or word. $ 4) Display the line number with output lines $ grep -l 'root' /etc/fstab /etc/passwd /etc/mtab Let’s assume we want to list the files names which contains word ‘root’, to do so use ‘-l’ option in grep command followed by word (pattern) and files. $ sudo grep linuxtechi /etc/passwd /etc/shadow /etc/gshadowģ) Print file names that matches the pattern Run following to search ‘linuxtechi’ word in /etc/passwd, /etc/shadow and /etc/gshadow files. $ 2) Searching pattern in the multiple filesĪ word or a pattern can be searched in multiple files using grep command. Search a word “nobody” word in the file /etc/passwd file, $ grep nobody /etc/passwd When we run grep command followed by search string or pattern then it will print the matching line of a file. I believe lp cares about what the device is.Without any further delay, let’s deep dive into grep command examples. I Believe lp cares about what the device is. Is only one file (or only standard input) to search.” Output The sample was printed via the internet. Suppress the prefixing of file names on output. Command grep -i -hn ‘lp’ Sample*Īccording to the Linux Docs, the -h flag does the following, In that case we can use the -h parameter which can be used to hide the filenames. We simply want the string where a particular pattern is found and we just need to print that string. The only concern for this particular scenario is that we don’t want the filename to be printed along with the output. Sample3: I believe lp cares about what the device is. Sample2: This was printed via the internet. Sample1: I believe lp cares about what the device is. Sample1: This was printed via the internet. Sample: I believe lp cares about what the device is. Command grep -i ‘lp’ Sample* Output Sample: The sample was printed via the internet. Let’s see a simple example where we will grep a pattern in all the files that are present in a directory. The grep command performs a case-insensitive search for words in a file. We know that we can make use of the grep command to search for a particular pattern of characters in all the lines of a file or multiple files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |