CCWC (Custom Word Count)
A Go implementation of the Unix/Linux wc
command that counts bytes, lines, words, and characters in text files.
Features
- Count bytes in a file (
-c
) - Count lines in a file (
-l
) - Count words in a file (
-w
) - Count characters in a file (
-m
) - Support for reading from both files and standard input (stdin)
- Default mode that displays line, word, and byte counts together
Installation
- Ensure you have Go installed on your system
- Navigate to the project directory:
cd /Users/rokib/Developer/go_projects/bgce-archive/mini-projects/ccwc
- Build the project:
go build -o ccwc ./cmd/ccwc
Usage
Basic Usage
./ccwc [options] [filename]
If no filename is provided, ccwc reads from standard input.
Options
-c
: Print the byte count-l
: Print the line count-w
: Print the word count-m
: Print the character count (locale-dependent)
If no options are provided, ccwc displays line count, word count, and byte count.
Examples
- Count bytes in a file:
./ccwc -c testdata/test.txt
- Count lines in a file:
./ccwc -l testdata/test.txt
- Count words in a file:
./ccwc -w testdata/test.txt
- Count characters in a file:
./ccwc -m testdata/test.txt
- Default output (lines, words, and bytes):
./ccwc testdata/test.txt
- Read from standard input:
cat testdata/test.txt | ./ccwc -m
Project Structure
mini-projects/ccwc/
├── cmd/
│ └── ccwc/
│ └── main.go # Main application entry point
├── internal/
│ └── processor/
│ └── processor.go # Core processing functions
├── testdata/ # Test files directory
│ └── test.txt
├── go.mod # Go module file
└── README.md # This file
Implementation Details
The project is organized into two main components:
-
Processor Package (
internal/processor/processor.go
):- Contains core counting functions
- Implements byte, line, word, and character counting logic
- Uses efficient buffered I/O operations
-
Main Package (
cmd/ccwc/main.go
):- Handles command-line argument parsing
- Manages file operations
- Coordinates counting operations
- Formats and displays output
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.