# Getting Started with hawk ๐ฆ
**5-minute introduction to hawk's data processing capabilities**
hawk is a command-line tool that lets you explore and analyze data using a simple, unified query language. Whether you're working with JSON APIs, CSV files, YAML configs, or log files, hawk uses the same intuitive syntax.
## ๐ฆ Installation
Choose your preferred installation method:
### Homebrew (Recommended)
```bash
brew install kyotalab/tools/hawk
```
### Cargo (Rust)
```bash
cargo install hawk-data
```
### Verify Installation
```bash
hawk --version
# Output: hawk 0.2.2
```
## ๐ฏ Your First hawk Command
Let's start with a simple example. Create a test file:
```bash
cat << 'EOF' > users.json
{
"users": [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
]
}
EOF
```
Now run your first hawk command:
```bash
hawk '.users[0].name' users.json
```
**Output:** `Alice`
**What happened?**
- `.users` โ access the "users" field
- `[0]` โ get the first element of the array
- `.name` โ get the "name" field from that element
## ๐๏ธ Basic Building Blocks
### 1. Field Access
```bash
# Access a field
hawk '.name' data.json
# Access nested fields
hawk '.user.profile.email' data.json
# Access array elements
hawk '.items[0]' data.json
```
### 2. Array Operations
```bash
# Get all array elements
hawk '.users[]' users.json
# Access specific fields from all elements
hawk '.users[].name' users.json
```
### 3. Filtering with select()
```bash
# Find users older than 25
# Find users named "Alice"
### 4. Counting and Aggregation
```bash
# Count total users
# Average age
## ๐งช Hands-on Examples
Let's work through progressively complex examples with sample data.
### Example 1: JSON Data Analysis
Create a sample dataset:
```bash
cat > sales.json << 'EOF'
{
"sales": [
{"product": "Laptop", "price": 1200, "quantity": 3, "region": "North"},
{"product": "Mouse", "price": 25, "quantity": 50, "region": "South"},
{"product": "Keyboard", "price": 80, "quantity": 20, "region": "North"},
{"product": "Monitor", "price": 300, "quantity": 10, "region": "South"}
]
}
EOF
```
**Basic Operations:**
```bash
# See all products
hawk '.sales[].product' sales.json
# Find expensive items (>$100)
# Count items by region
# Average price by region
### Example 2: CSV Data Processing
Create a CSV file:
```bash
cat > employees.csv << 'EOF'
name,age,department,salary
Alice,30,Engineering,95000
Bob,25,Marketing,75000
Carol,35,Engineering,105000
David,28,Sales,80000
EOF
```
**CSV Operations:**
```bash
# See all names
hawk '.[].name' employees.csv
# Find engineers
# Average salary by department
# Count employees by department
### Example 3: Text/Log Processing
Create a sample log file:
```bash
cat > app.log << 'EOF'
2024-01-15 09:00:01 INFO Application started
2024-01-15 09:00:15 ERROR Database connection failed
2024-01-15 09:00:16 INFO Retrying connection
2024-01-15 09:01:20 WARN High memory usage: 85%
2024-01-15 09:01:45 ERROR Timeout occurred
EOF
```
**Text Processing Operations:**
```bash
# Process as text (use -t flag for logs)
# Find all ERROR lines
# Extract timestamps
# Extract log levels
## ๐ง String Operations
hawk includes powerful string manipulation:
```bash
# Text transformation
# String splitting with index access (NEW!)
# Multiple field processing
"first": "john",
"last": "doe"
}
EOF
```
## ๐ Understanding Output Formats
hawk automatically chooses the best output format:
```bash
# Single value โ simple output
hawk '.users[0].name' users.json
# Output: Alice
# Array of objects โ table format
hawk '.users[]' users.json
# Output: Formatted table with columns
```
You can force specific formats:
```bash
hawk '.users[]' --format json users.json # Force JSON
hawk '.users[]' --format table users.json # Force table
hawk '.users[].name' --format list users.json # Force list
```
## ๐ฏ Common Patterns
### Data Exploration
```bash
# Understand data structure
# Count total records
# See unique values
hawk '.field_name[] | unique' data.json
```
### Filtering and Aggregation
```bash
# Filter โ count pattern
# Group โ aggregate pattern
# Filter โ group โ aggregate pattern
### Text Processing
```bash
# Extract โ unique pattern
# Filter โ extract pattern
# Clean โ transform pattern
## ๐จ When to Use --text Flag
Use the `--text` flag when processing files that might be misdetected:
```bash
# For log files that look like YAML
# For any text file you want to process line-by-line
## ๐ Next Steps
Now that you know the basics, explore these guides:
### **Immediate Next Steps**
1. **[String Operations Guide](string-operations.md)** - Master text processing
2. **[Query Language Reference](query-language.md)** - Complete syntax guide
3. **[Log Analysis Examples](examples/log-analysis.md)** - Real-world log processing
### **By Use Case**
- **Data Analysis**: [Data Analysis Guide](data-analysis.md)
- **DevOps**: [DevOps Workflows](examples/devops-workflows.md)
- **API Work**: [API Exploration](examples/api-exploration.md)
### **Advanced Topics**
- **Performance**: [Optimization Tips](advanced/performance.md)
- **Complex Workflows**: [Custom Workflows](advanced/custom-workflows.md)
## ๐ Quick Reference Card
### Essential Commands
```bash
# Field access
hawk '.field' data.json
hawk '.array[0]' data.json
hawk '.array[]' data.json
# Filtering
# Aggregation
# Grouping
# Text processing
# String operations
```
### Data Types
- **JSON**: `data.json` โ auto-detected
- **YAML**: `config.yaml` โ auto-detected
- **CSV**: `data.csv` โ auto-detected
- **Text**: `file.txt` โ use `-t` flag for line processing
## ๐ก Pro Tips
1. **Start Simple**: Begin with basic field access, then add complexity
2. **Use `info`**: Always start data exploration with `hawk '. | info' file`
3. **Test in Steps**: Build complex queries incrementally
4. **Use `--text`**: When in doubt with text files, use the `-t` flag
5. **Read Error Messages**: hawk provides helpful error context
## ๐ You're Ready!
You now know enough hawk to be productive! The key is to start with simple operations and gradually build more complex queries as you become comfortable with the syntax.
**Remember**: hawk uses the same syntax across all data formats, so skills learned with JSON work with CSV, YAML, and text files.
Happy data exploring! ๐ฆ
---
**Quick Links:**
- [String Operations](string-operations.md) - Text processing guide
- [Examples](../examples/README.md) - Real-world use cases