Search

Linux File filter commands sort wc and grep

The Linux sort command

The Linux sort command can be used to sort the contents of a file in a number of ways. By default, the Linux sort command sorts the contents in alphabetical order depending on the first letter in each line. For example, the sort /etc/passwd command would sort all users by username.

Important options of the sort are

• -b (Ignores spaces at beginning of the line)
• -d (Uses dictionary sort order and ignores the punctuation)
• -f (Ignores caps)
• -i (Ignores nonprinting control characters)
• -m (Merges two or more input files into one sorted output)
• -r (Sorts in reverse order)
• -u (If line is duplicated only display once)

The Linux wc command

The Linux wc (word count) command, can return the number of lines, words, and characters in a file. Important options of the Linux wc command are

• -c (Print the byte counts)
• -m (Print the character counts)
• -l (Print the new line counts)
• -w (Print the word counts)

The Linux grep command

The Linux grep command uses a search term to look through a file. The Linux grep command can parse lines based on text or RegEx. By default the Linux grep command search for patterns case-sensitively.

Important options of the grep command

• -e (Used to specify a pattern)
• -i (Case insensitive search)
• -c (Print a count of matching lines)
• -v (invert search - Returns lines that do not match, instead of lines that match.)
• -w (Matches only when the input text consists of full words)
• -x (Should match the entire line)
• --color (Colorize output)
• -l (Instead of normal output, prints just the names of input files containing the pattern)
• -L (Prints the names of input files that contain no matches)
• -o (Prints only the text that matches, instead of the whole line of input)
• -q (Suppresses output. Useful in finding the exit status (0 for success if a match is found, 1 for no match found, 2 if the program cannot run because of an error)).

"^" character is used as anchor, to find the lines which begin which the following text. Example – “^RedHat”

"$" character is used as anchor, to find the lines which end with the preceding text. Example – "RHEL5$"

To search for lines which contains numbers, use RegEx expression range "[0-9]"

To search for lines which contain small case letters, use RegEx expression range "[a-z]"

To search for lines which contain caps letters, use RegEx expression range "[A-Z]"

Note: You should be familiar with basic Linux command to learn this lesson. Click the following link to learn basic Linux commands.

Related Tutorials