How to use wc, fgrep and cal commands

Introduction:

wc Command for (Count Number of Lines, Words, and Characters) In Linux operating systems, the wc command allows you to count the number of lines, words, characters, and bytes of each given file or standard input and print the result.

wc stands for word count. As the name implies, it is mainly used for counting purpose.

  • It is used to find out number of linesword countbyte and characters count.
  • By default it displays four-columnar output.
  • First column shows number of lines present in a file specified, second column shows number of words present in the file, third column shows number of characters present in file and fourth column itself is the file name which are given as argument.

Command:

wc name_of_file

output:

1

To specify the output of the number of lines in file.

Command:

wc -l name_of_file

Output:

2

To specify the output of the number of characters in a file.

Command:

wc -c name_of_file

Output:

3

To specify the output of the number of words containing in a file.

Command:

wc -w name_of_file

Output:

4

To specify the -m option ‘wc’ command displays count of characters from a file.

Command:

wc -m name_of_file

Output:

5

2- cal 

This command is to show or print the calendar of the current month.

Using this command we can print the calendar of the any year or month.

Command:

cal

output:

6

To print the calendar of the specific month type the following command.

command:

cal name_of_month name_of_year

output:

6

To print the calendar of the specific year type the following command.

command:

cal year

output:

7

3- fgrep

The fgrep filter is used to search for the fixed-character strings in a file. This command is useful when we need to search for strings which contain lots of regular expression meta characters, such as “^”, “$”, etc.

Options with Description:

  • -c : It is used to print only a count of the lines which contain the pattern.
  • -h : Used to display the matched lines.
  • -i : During comparision, it will ignore upper/lower case distinction.
  • -l : Used to print the names of files with matching lines once, separated by new-lines. It will not repeat the names of files when the pattern is found more than once.
  • -n : It is used precede each line by its line number in the file (first line is 1).
  • -s : It will only display the error messages.
  • -v : Print all lines except those contain the pattern.
  • -x : Print only lines matched entirely.
  • -e pattern_list : Search for a string in pattern-list (useful when the string begins with a “-“).
  • -f pattern-file : Take the list of patterns from pattern-file.
  • pattern : Specify a pattern to be used during the search for input.
  • file : A path name of a file to be searched for the patterns. If no file operands are specified, the standard input will be used.

To display the count of number of matches. We can find the number of lines that match the given string.

Command:

fgrep -c ‘pattern want to find’ filename

output:

8

 

In this blog we have seen the usage of fgrep, cal and wc commands. These are linux basics commands which we use on daily bases and for perform daily operations.

Leave a Reply

Your email address will not be published.