Grep ignore case comment9/9/2023 Well, that is easy, just run another grep command: # grep -v '^#' /etc/sudoers | grep -v '^$' Now, we want to get rid of the blank (empty) lines. The following output is produced: # grep -v '^#' /etc/sudoersĭefaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"ĭefaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"ĭefaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"ĭefaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"ĭefaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"ĭefaults secure_path = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin So, first, let’s remove the lines containing the comments. This file contains many comments, but we are only interested in lines that have no comments, and we also want to get rid of the empty lines. While we’re still on grep, let’s examine the /etc/sudoers file. In the next article, we’ll use sed to achieve the same thing we did here with grep. To do this, we’d need a file editor like ed. While grep can format the output on the screen, this command is unable to modify a file in place. If you want to send the output (without comments) to another file instead, you’d use: $ grep -v '^#' /etc/fstab > ~/fstab_without_comment The grep command is perfectly capable of reading files, so instead, you can use something like this to ignore lines that contain comments: $ grep -v '^#' /etc/fstab However, you don’t need cat here (avoid Useless Use of Cat). One option is the cat command: $ cat /etc/fstab | grep -v '^#' The comments are marked by the hash ( #), and we want to ignore them when printed. #/dev/mapper/VGCRYPTO-SWAP swap swap defaults,vice-timeout=0 0 0 UUID=e9de0f73-ddddd-4d45-a9ba-1ffffa /boot ext4 defaults 1 2 dev/mapper/VGCRYPTO-ROOT / ext4 defaults,vice-timeout=0 1 1 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # Accessible filesystems, by reference, are maintained under '/dev/disk' # Created by anaconda on Thu Oct 27 05:06:06 2016 The /etc/fstab, file for example, has: $ cat /etc/fstab Many configuration files in *nix contain comments, which describe different settings within the configuration file. Now that we’ve scratched the surface of grep, let’s work on some real-world scenarios. If you want to know more about metacharacters, see. The ^ and $ are called metacharacters and should be escaped with a backslash ( \) when we want to match these characters literally. Ironically, we’ll use grep to grep for the option as follows: Let’s see if grep offers an option to count lines in a given file. Ok, so we said the words file contained 479,826 lines, but how do we know that? Remember, we talked about man pages earlier. When we list the contents of the words file, we see the following output: $ cat /usr/share/dict/words This file contains 479,826 words and is provided by the words package. 1 root root 11 /usr/share/dict/words -> linux.words grep basicsĭuring this part of the article, we use the words file, which you can find at the following location: $ ls -l /usr/share/dict/words With man pages at our disposal, we now can use grep and explore its options. On Red Hat Enterprise Linux-based systems, we can run the following to list grep's documentation files: $ rpm -qd grep Many (if not all) Unix tools provide man pages during install. The best way to understand the working of a program or utility is to consult its man page. In an upcoming article, we will also see how to use sed (Stream Editor) to manipulate a stream. In this article, we will cover the grep command to search for patterns, whether found in files or coming from a stream (a file or input comping from a pipe, or |). There are many tools we can use in *nix-based systems to find and manipulate text. How well do you know Linux? Take a quiz and get a badge.Linux system administration skills assessment.A guide to installing applications on Linux.Download RHEL 9 at no charge through the Red Hat Developer program.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |