MKBWC is a command line tool created using Go to compute common metrics for a text file. The command computes the below metrics:
- Number of lines present in the file
- Number of words present in the file
- Number of characters present in the file
- Number of bytes present in the file
This is my solution to the challenge posted at Coding Challenges to create my own implementation of the wc linux command.
To build and run the project, execute the following commands:
# Convert the build.sh file into an executable by running the below command.
chmod +x ./build.sh
# Below command builds the project and generates the executable file.
./build.sh The Commands and Outputs section of this file gives different ways of running the executable file with various command-line options.
To run the test scripts, run the below command on the terminal.
go test ./lib/metrics -v -cover- The -v command line option enables the verbose logs to be printed as the test scripts are being executed.
- The -cover command line option displays the code coverage information for the test cases executed.
This section contains various example variations for the mkbwc command and the outputs displayed on screen for each of these variations.
In this example, we display the help message associated with the MKBWC command to understand the various options available for us to use with the command.
Input Command
./mkbwc -hOutput displayed
Usage: ./mkbwc [options] filename(s)
Options available:
-c Output the number of bytes in the given file
-h Show the help message
-l Output the number of lines in the given file
-m Output the number of characters in the given file
-w Output the number of words in the given fileIn this example, we fetch the number of lines, words and bytes present in file - test-one.txt. If there are no options provided in the command line, the program computes the 3 aforementioned metrics.
Input Command
./mkbwc ./test-files/test-one.txtOutput displayed
1 41 285 test-one.txtIn this example, we fetch the number of lines present in file - test-two.txt.
Input Command
./mkbwc -l ./test-files/test-two.txtOutput displayed
5 test-two.txtIn this example, we fetch the number of bytes of content present in file - test-two.txt.
Input Command
./mkbwc -c ./test-files/test-two.txtOutput displayed
1663 test-two.txtIn this example, we chain the mkbwc command with the find command such that the output of the find command which yields the list of all files in the given folder, as an input to the mkbwc command.
Input Command
find "./test-files" -type f | ./mkbwc -mOutput displayed
1663 test-two.txt
285 test-one.txt
547 test-three.txtPlease note that, if there are any filename(s) provided with the mkbwc command, only those files will be processed and the input from find command will be ignored. So, it is recommended to mention only the flags with mkbwc, when it is being chained with other linux commands for getting input files.