@hybridlabor-api/aos 4.8.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/agents.md +1 -1
- package/.agents/nodes.json +1 -1
- package/.agents/vendor-manifest.json +23 -1
- package/.claude/agents/godmode-media-eventtech.md +1 -1
- package/.claude/hooks/conventional-commits.mjs +125 -0
- package/.claude/hooks/env-file-protection.mjs +105 -0
- package/.claude/hooks/go-gate.mjs +101 -81
- package/.claude/settings.json +13 -0
- package/.opencode/agents/godmode-media-eventtech.md +1 -1
- package/.opencode/plugins/bdb-aos.js +31 -4
- package/README.de.md +1 -1
- package/README.md +1 -1
- package/README.pt.md +1 -1
- package/THIRD_PARTY_NOTICES.md +88 -0
- package/docs/skills_table.md +1 -1
- package/installer.js +174 -46
- package/package.json +6 -2
- package/packages/aos-cli/README.md +80 -0
- package/packages/aos-cli/bin/aos-cli.mjs +134 -0
- package/packages/aos-cli/core-skills.json +12 -0
- package/packages/aos-cli/extensions/aos.ts +321 -0
- package/packages/aos-cli/package-lock.json +1923 -0
- package/packages/aos-cli/package.json +29 -0
- package/packages/aos-cli/scripts/check-theme.mjs +63 -0
- package/packages/aos-cli/themes/aos.json +97 -0
- package/scripts/build-plugin-manifest.mjs +131 -0
- package/scripts/validate-skills.mjs +81 -6
- package/skills/basic/ao-orchestrator/SKILL.md +116 -0
- package/skills/global_config/agenttrail/SKILL.md +6 -1
- package/skills/global_config/ask-tim/SKILL.md +4 -4
- package/skills/global_config/bash-script-generator/SKILL.md +201 -0
- package/skills/global_config/bash-script-generator/assets/templates/standard-template.sh +96 -0
- package/skills/global_config/bash-script-generator/docs/bash-scripting-guide.md +729 -0
- package/skills/global_config/bash-script-generator/docs/generation-best-practices.md +193 -0
- package/skills/global_config/bash-script-generator/docs/script-patterns.md +566 -0
- package/skills/global_config/bash-script-generator/docs/text-processing-guide.md +437 -0
- package/skills/global_config/bash-script-generator/examples/log-analyzer.sh +92 -0
- package/skills/global_config/bash-script-generator/scripts/generate_script_template.sh +123 -0
- package/skills/global_config/bash-script-generator/scripts/run_ci_checks.sh +172 -0
- package/skills/global_config/bash-script-generator/scripts/test_generator.sh +413 -0
- package/skills/global_config/bash-script-validator/SKILL.md +249 -0
- package/skills/global_config/bash-script-validator/docs/awk-reference.md +449 -0
- package/skills/global_config/bash-script-validator/docs/bash-reference.md +468 -0
- package/skills/global_config/bash-script-validator/docs/common-mistakes.md +623 -0
- package/skills/global_config/bash-script-validator/docs/grep-reference.md +395 -0
- package/skills/global_config/bash-script-validator/docs/regex-reference.md +391 -0
- package/skills/global_config/bash-script-validator/docs/sed-reference.md +454 -0
- package/skills/global_config/bash-script-validator/docs/shell-reference.md +463 -0
- package/skills/global_config/bash-script-validator/docs/shellcheck-reference.md +399 -0
- package/skills/global_config/bash-script-validator/examples/bad-bash.sh +55 -0
- package/skills/global_config/bash-script-validator/examples/bad-shell.sh +54 -0
- package/skills/global_config/bash-script-validator/examples/good-bash.sh +71 -0
- package/skills/global_config/bash-script-validator/examples/good-shell.sh +69 -0
- package/skills/global_config/bash-script-validator/scripts/run_ci_checks.sh +23 -0
- package/skills/global_config/bash-script-validator/scripts/shellcheck_wrapper.sh +174 -0
- package/skills/global_config/bash-script-validator/scripts/test_validate.sh +446 -0
- package/skills/global_config/bash-script-validator/scripts/validate.sh +512 -0
- package/skills/global_config/ci-pipeline/SKILL.md +135 -0
- package/skills/global_config/dispatching-parallel-agents/SKILL.md +170 -0
- package/skills/global_config/dockerfile-generator/SKILL.md +1038 -0
- package/skills/global_config/dockerfile-generator/examples/example.dockerignore +95 -0
- package/skills/global_config/dockerfile-generator/examples/golang-distroless.Dockerfile +34 -0
- package/skills/global_config/dockerfile-generator/examples/java-springboot.Dockerfile +45 -0
- package/skills/global_config/dockerfile-generator/examples/nextjs-production.Dockerfile +49 -0
- package/skills/global_config/dockerfile-generator/examples/nodejs-multistage.Dockerfile +55 -0
- package/skills/global_config/dockerfile-generator/examples/python-fastapi.Dockerfile +48 -0
- package/skills/global_config/dockerfile-generator/references/language_specific_guides.md +510 -0
- package/skills/global_config/dockerfile-generator/references/multistage_builds.md +570 -0
- package/skills/global_config/dockerfile-generator/references/optimization_patterns.md +492 -0
- package/skills/global_config/dockerfile-generator/references/security_best_practices.md +375 -0
- package/skills/global_config/dockerfile-generator/scripts/generate_dockerignore.sh +199 -0
- package/skills/global_config/dockerfile-generator/scripts/generate_golang.sh +172 -0
- package/skills/global_config/dockerfile-generator/scripts/generate_java.sh +185 -0
- package/skills/global_config/dockerfile-generator/scripts/generate_nodejs.sh +279 -0
- package/skills/global_config/dockerfile-generator/scripts/generate_python.sh +218 -0
- package/skills/global_config/dockerfile-generator/scripts/test_generator.sh +210 -0
- package/skills/global_config/dockerfile-validator/SKILL.md +300 -0
- package/skills/global_config/dockerfile-validator/examples/.dockerignore.example +34 -0
- package/skills/global_config/dockerfile-validator/examples/bad-example.Dockerfile +37 -0
- package/skills/global_config/dockerfile-validator/examples/golang-distroless.Dockerfile +50 -0
- package/skills/global_config/dockerfile-validator/examples/good-example.Dockerfile +52 -0
- package/skills/global_config/dockerfile-validator/examples/python-optimized.Dockerfile +53 -0
- package/skills/global_config/dockerfile-validator/examples/security-issues.Dockerfile +42 -0
- package/skills/global_config/dockerfile-validator/references/docker_best_practices.md +348 -0
- package/skills/global_config/dockerfile-validator/references/optimization_guide.md +473 -0
- package/skills/global_config/dockerfile-validator/references/security_checklist.md +208 -0
- package/skills/global_config/dockerfile-validator/scripts/dockerfile-validate.sh +699 -0
- package/skills/global_config/dockerfile-validator/scripts/test_validate.sh +35 -0
- package/skills/global_config/dockerfile-validator/tests/fixtures/copy-before-yarn-lock-read.Dockerfile +6 -0
- package/skills/global_config/dockerfile-validator/tests/fixtures/copy-before-yarn.Dockerfile +6 -0
- package/skills/global_config/dockerfile-validator/tests/fixtures/from-platform-nonroot.Dockerfile +7 -0
- package/skills/global_config/dockerfile-validator/tests/test_regression.sh +164 -0
- package/skills/global_config/finishing-a-development-branch/SKILL.md +228 -0
- package/skills/global_config/github-actions-generator/SKILL.md +353 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/composite/action.yml +82 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/docker/Dockerfile +25 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/docker/action.yml +42 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/docker/entrypoint.sh +27 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/javascript/action.yml +33 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/javascript/index.js +50 -0
- package/skills/global_config/github-actions-generator/assets/templates/action/javascript/package.json +27 -0
- package/skills/global_config/github-actions-generator/assets/templates/workflow/basic_workflow.yml +242 -0
- package/skills/global_config/github-actions-generator/assets/templates/workflow/reusable_workflow.yml +106 -0
- package/skills/global_config/github-actions-generator/examples/README.md +147 -0
- package/skills/global_config/github-actions-generator/examples/actions/setup-node-cached/action.yml +93 -0
- package/skills/global_config/github-actions-generator/examples/caching/docker-buildkit.yml +256 -0
- package/skills/global_config/github-actions-generator/examples/security/dependency-review.yml +62 -0
- package/skills/global_config/github-actions-generator/examples/security/sbom-attestation.yml +119 -0
- package/skills/global_config/github-actions-generator/examples/triggers/chatops-commands.yml +475 -0
- package/skills/global_config/github-actions-generator/examples/triggers/repository-dispatch.yml +418 -0
- package/skills/global_config/github-actions-generator/examples/triggers/workflow-orchestration.yml +404 -0
- package/skills/global_config/github-actions-generator/examples/workflows/docker-build-push.yml +68 -0
- package/skills/global_config/github-actions-generator/examples/workflows/go-ci.yml +161 -0
- package/skills/global_config/github-actions-generator/examples/workflows/monorepo-ci.yml +340 -0
- package/skills/global_config/github-actions-generator/examples/workflows/multi-environment-deploy.yml +406 -0
- package/skills/global_config/github-actions-generator/examples/workflows/nodejs-ci.yml +122 -0
- package/skills/global_config/github-actions-generator/examples/workflows/python-ci.yml +157 -0
- package/skills/global_config/github-actions-generator/examples/workflows/scheduled-tasks.yml +376 -0
- package/skills/global_config/github-actions-generator/references/advanced-triggers.md +917 -0
- package/skills/global_config/github-actions-generator/references/best-practices.md +755 -0
- package/skills/global_config/github-actions-generator/references/common-actions.md +715 -0
- package/skills/global_config/github-actions-generator/references/custom-actions.md +320 -0
- package/skills/global_config/github-actions-generator/references/expressions-and-contexts.md +688 -0
- package/skills/global_config/github-actions-generator/references/modern-features.md +421 -0
- package/skills/global_config/github-actions-generator/scripts/test_generator.sh +344 -0
- package/skills/global_config/github-actions-templates/SKILL.md +7 -0
- package/skills/global_config/github-actions-validator/SKILL.md +576 -0
- package/skills/global_config/github-actions-validator/examples/README.md +88 -0
- package/skills/global_config/github-actions-validator/examples/outdated-versions.yml +76 -0
- package/skills/global_config/github-actions-validator/examples/valid-ci.yml +79 -0
- package/skills/global_config/github-actions-validator/examples/with-errors.yml +47 -0
- package/skills/global_config/github-actions-validator/references/act_usage.md +233 -0
- package/skills/global_config/github-actions-validator/references/action_versions.md +122 -0
- package/skills/global_config/github-actions-validator/references/actionlint_usage.md +343 -0
- package/skills/global_config/github-actions-validator/references/common_errors.md +512 -0
- package/skills/global_config/github-actions-validator/references/modern_features.md +384 -0
- package/skills/global_config/github-actions-validator/references/runners.md +317 -0
- package/skills/global_config/github-actions-validator/scripts/install_tools.sh +113 -0
- package/skills/global_config/github-actions-validator/scripts/validate_workflow.sh +910 -0
- package/skills/global_config/github-actions-validator/tests/test_validate_workflow.sh +237 -0
- package/skills/global_config/makefile-generator/SKILL.md +614 -0
- package/skills/global_config/makefile-generator/assets/templates/.gitkeep +1 -0
- package/skills/global_config/makefile-generator/docs/makefile-structure.md +530 -0
- package/skills/global_config/makefile-generator/docs/optimization-guide.md +784 -0
- package/skills/global_config/makefile-generator/docs/patterns-guide.md +642 -0
- package/skills/global_config/makefile-generator/docs/security-guide.md +361 -0
- package/skills/global_config/makefile-generator/docs/targets-guide.md +642 -0
- package/skills/global_config/makefile-generator/docs/variables-guide.md +596 -0
- package/skills/global_config/makefile-generator/scripts/add_standard_targets.sh +539 -0
- package/skills/global_config/makefile-generator/scripts/generate_makefile_template.sh +690 -0
- package/skills/global_config/makefile-generator/test/test_helper_scripts.sh +190 -0
- package/skills/global_config/makefile-validator/SKILL.md +244 -0
- package/skills/global_config/makefile-validator/docs/bake-tool.md +1000 -0
- package/skills/global_config/makefile-validator/docs/best-practices.md +858 -0
- package/skills/global_config/makefile-validator/docs/common-mistakes.md +944 -0
- package/skills/global_config/makefile-validator/examples/bad-makefile.mk +77 -0
- package/skills/global_config/makefile-validator/examples/good-makefile.mk +103 -0
- package/skills/global_config/makefile-validator/scripts/test_validate.sh +382 -0
- package/skills/global_config/makefile-validator/scripts/validate_makefile.sh +712 -0
- package/skills/global_config/{MCP_Manage → mcp-manage}/SKILL.md +3 -3
- package/skills/global_config/requesting-code-review/SKILL.md +98 -0
- package/skills/global_config/requesting-code-review/code-reviewer.md +198 -0
- package/skills/global_config/using-git-worktrees/SKILL.md +170 -0
- package/skills/global_config/verification-before-completion/SKILL.md +123 -0
- package/skills/global_config/writing-plans/SKILL.md +126 -46
- package/skills/global_config/writing-plans-legacy/SKILL.md +139 -0
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
# Text Processing Guide
|
|
2
|
+
|
|
3
|
+
Guide for choosing and using grep, awk, sed, and other text processing tools effectively in bash scripts.
|
|
4
|
+
|
|
5
|
+
## Decision Tree: Which Tool to Use?
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
Is it a simple pattern match/filter?
|
|
9
|
+
├─ YES → Use grep
|
|
10
|
+
└─ NO
|
|
11
|
+
├─ Is it field/column-based data?
|
|
12
|
+
│ └─ YES → Use awk
|
|
13
|
+
└─ NO
|
|
14
|
+
├─ Is it find-and-replace or deletion?
|
|
15
|
+
│ └─ YES → Use sed
|
|
16
|
+
└─ NO
|
|
17
|
+
└─ Complex processing → Use awk or consider Python/Perl
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## grep: Pattern Matching and Filtering
|
|
21
|
+
|
|
22
|
+
### When to Use grep
|
|
23
|
+
- Searching for patterns in files
|
|
24
|
+
- Filtering lines by regex
|
|
25
|
+
- Simple line extraction
|
|
26
|
+
- Counting matches
|
|
27
|
+
- Finding files containing patterns
|
|
28
|
+
|
|
29
|
+
### Common grep Patterns
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Basic search
|
|
33
|
+
grep "pattern" file.txt
|
|
34
|
+
|
|
35
|
+
# Case-insensitive
|
|
36
|
+
grep -i "error" log.txt
|
|
37
|
+
|
|
38
|
+
# Invert match (lines NOT containing pattern)
|
|
39
|
+
grep -v "DEBUG" log.txt
|
|
40
|
+
|
|
41
|
+
# Count matches
|
|
42
|
+
grep -c "ERROR" log.txt
|
|
43
|
+
|
|
44
|
+
# Show line numbers
|
|
45
|
+
grep -n "TODO" *.sh
|
|
46
|
+
|
|
47
|
+
# Extended regex (ERE)
|
|
48
|
+
grep -E "(error|fail|critical)" log.txt
|
|
49
|
+
|
|
50
|
+
# Recursive directory search
|
|
51
|
+
grep -r "function_name" src/
|
|
52
|
+
|
|
53
|
+
# Show filename only
|
|
54
|
+
grep -l "pattern" *.txt
|
|
55
|
+
|
|
56
|
+
# Show context (lines before/after)
|
|
57
|
+
grep -A 2 -B 2 "ERROR" log.txt # 2 lines after and before
|
|
58
|
+
|
|
59
|
+
# Multiple patterns
|
|
60
|
+
grep -e "error" -e "fail" log.txt
|
|
61
|
+
|
|
62
|
+
# Read patterns from file
|
|
63
|
+
grep -f patterns.txt input.txt
|
|
64
|
+
|
|
65
|
+
# Whole word match
|
|
66
|
+
grep -w "test" file.txt # Matches "test" but not "testing"
|
|
67
|
+
|
|
68
|
+
# Fixed string (not regex)
|
|
69
|
+
grep -F "a.b" file.txt # Matches literal "a.b", not regex
|
|
70
|
+
|
|
71
|
+
# Binary file handling
|
|
72
|
+
grep -a "pattern" binary_file # Treat binary as text
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### grep for Log Analysis
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Extract error messages from last hour
|
|
79
|
+
find /var/log -name "*.log" -mmin -60 -exec grep "ERROR" {} +
|
|
80
|
+
|
|
81
|
+
# Count errors by type
|
|
82
|
+
grep "ERROR" app.log | cut -d':' -f3 | sort | uniq -c | sort -rn
|
|
83
|
+
|
|
84
|
+
# Find errors excluding known issues
|
|
85
|
+
grep "ERROR" app.log | grep -v -f known_errors.txt
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## awk: Field-Based Text Processing
|
|
89
|
+
|
|
90
|
+
### When to Use awk
|
|
91
|
+
- Processing structured data (CSV, logs, tables)
|
|
92
|
+
- Extracting specific fields
|
|
93
|
+
- Performing calculations
|
|
94
|
+
- Generating reports
|
|
95
|
+
- Complex conditional logic on fields
|
|
96
|
+
|
|
97
|
+
### awk Basics
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
# Print specific fields (space-delimited by default)
|
|
101
|
+
awk '{print $1, $3}' file.txt
|
|
102
|
+
|
|
103
|
+
# Custom delimiter
|
|
104
|
+
awk -F',' '{print $1, $3}' data.csv
|
|
105
|
+
awk -F':' '{print $1}' /etc/passwd
|
|
106
|
+
|
|
107
|
+
# Multiple delimiters
|
|
108
|
+
awk -F'[,:]' '{print $1}' file.txt
|
|
109
|
+
|
|
110
|
+
# Print last field
|
|
111
|
+
awk '{print $NF}' file.txt
|
|
112
|
+
|
|
113
|
+
# Print all but first field
|
|
114
|
+
awk '{$1=""; print $0}' file.txt
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### awk Conditionals
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
# Print lines where field 3 > 100
|
|
121
|
+
awk '$3 > 100' data.txt
|
|
122
|
+
|
|
123
|
+
# Print lines where field matches pattern
|
|
124
|
+
awk '$2 ~ /error/' log.txt
|
|
125
|
+
|
|
126
|
+
# Print lines where field does NOT match
|
|
127
|
+
awk '$2 !~ /DEBUG/' log.txt
|
|
128
|
+
|
|
129
|
+
# Multiple conditions
|
|
130
|
+
awk '$3 > 100 && $4 < 500' data.txt
|
|
131
|
+
|
|
132
|
+
# If-else logic
|
|
133
|
+
awk '{if ($3 > 100) print "High:", $0; else print "Low:", $0}' data.txt
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### awk Calculations
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
# Sum values in column 3
|
|
140
|
+
awk '{sum += $3} END {print sum}' numbers.txt
|
|
141
|
+
|
|
142
|
+
# Average
|
|
143
|
+
awk '{sum += $3; count++} END {print sum/count}' numbers.txt
|
|
144
|
+
|
|
145
|
+
# Find max value
|
|
146
|
+
awk 'BEGIN {max=0} {if ($1 > max) max=$1} END {print max}' numbers.txt
|
|
147
|
+
|
|
148
|
+
# Count lines matching condition
|
|
149
|
+
awk '$3 > 100 {count++} END {print count}' data.txt
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### awk Formatted Output
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
# Printf-style formatting
|
|
156
|
+
awk '{printf "Name: %-20s Age: %3d\n", $1, $2}' people.txt
|
|
157
|
+
|
|
158
|
+
# Tab-separated output
|
|
159
|
+
awk 'BEGIN {OFS="\t"} {print $1, $2, $3}' file.txt
|
|
160
|
+
|
|
161
|
+
# Custom output formatting
|
|
162
|
+
awk '{printf "%s: %10.2f\n", $1, $2}' data.txt
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### awk Built-in Variables
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
NF # Number of fields in current line
|
|
169
|
+
NR # Current line number
|
|
170
|
+
FNR # Line number in current file
|
|
171
|
+
FS # Input field separator
|
|
172
|
+
OFS # Output field separator
|
|
173
|
+
RS # Input record separator
|
|
174
|
+
ORS # Output record separator
|
|
175
|
+
FILENAME # Current filename
|
|
176
|
+
|
|
177
|
+
# Examples
|
|
178
|
+
awk '{print NR, NF, $0}' file.txt # Line number, field count, full line
|
|
179
|
+
awk 'NR==10' file.txt # Print line 10
|
|
180
|
+
awk 'NF > 5' file.txt # Lines with more than 5 fields
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### awk for Log Analysis
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
# Apache/Nginx access log analysis
|
|
187
|
+
# Extract status codes and count
|
|
188
|
+
awk '{print $9}' access.log | sort | uniq -c | sort -rn
|
|
189
|
+
|
|
190
|
+
# Summarize traffic by IP
|
|
191
|
+
awk '{ip[$1]++} END {for (i in ip) print ip[i], i}' access.log | sort -rn
|
|
192
|
+
|
|
193
|
+
# Calculate average response time (field 11)
|
|
194
|
+
awk '{sum += $11; count++} END {print sum/count}' access.log
|
|
195
|
+
|
|
196
|
+
# Extract requests by hour
|
|
197
|
+
awk '{print substr($4, 2, 14)}' access.log | uniq -c
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## sed: Stream Editing
|
|
201
|
+
|
|
202
|
+
### When to Use sed
|
|
203
|
+
- Find and replace operations
|
|
204
|
+
- Deleting specific lines
|
|
205
|
+
- In-place file editing
|
|
206
|
+
- Simple transformations
|
|
207
|
+
|
|
208
|
+
### sed Substitution
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
# Basic substitution (first occurrence per line)
|
|
212
|
+
sed 's/old/new/' file.txt
|
|
213
|
+
|
|
214
|
+
# Global substitution (all occurrences)
|
|
215
|
+
sed 's/old/new/g' file.txt
|
|
216
|
+
|
|
217
|
+
# Case-insensitive substitution
|
|
218
|
+
sed 's/old/new/gi' file.txt
|
|
219
|
+
|
|
220
|
+
# In-place editing
|
|
221
|
+
sed -i 's/old/new/g' file.txt
|
|
222
|
+
|
|
223
|
+
# Backup before in-place edit
|
|
224
|
+
sed -i.bak 's/old/new/g' file.txt
|
|
225
|
+
|
|
226
|
+
# Replace only on specific line
|
|
227
|
+
sed '5s/old/new/' file.txt
|
|
228
|
+
|
|
229
|
+
# Replace on lines matching pattern
|
|
230
|
+
sed '/ERROR/s/old/new/g' file.txt
|
|
231
|
+
|
|
232
|
+
# Use different delimiter
|
|
233
|
+
sed 's|/usr/local|/opt|g' file.txt
|
|
234
|
+
|
|
235
|
+
# Backreferences
|
|
236
|
+
sed 's/\([0-9]*\)-\([0-9]*\)/\2-\1/' file.txt
|
|
237
|
+
|
|
238
|
+
# Multiple substitutions
|
|
239
|
+
sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### sed Deletion
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
# Delete specific line
|
|
246
|
+
sed '5d' file.txt
|
|
247
|
+
|
|
248
|
+
# Delete range of lines
|
|
249
|
+
sed '5,10d' file.txt
|
|
250
|
+
|
|
251
|
+
# Delete lines matching pattern
|
|
252
|
+
sed '/pattern/d' file.txt
|
|
253
|
+
|
|
254
|
+
# Delete empty lines
|
|
255
|
+
sed '/^$/d' file.txt
|
|
256
|
+
|
|
257
|
+
# Delete lines NOT matching pattern
|
|
258
|
+
sed '/pattern/!d' file.txt
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### sed Line Operations
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
# Print specific line
|
|
265
|
+
sed -n '10p' file.txt
|
|
266
|
+
|
|
267
|
+
# Print range
|
|
268
|
+
sed -n '10,20p' file.txt
|
|
269
|
+
|
|
270
|
+
# Print lines matching pattern
|
|
271
|
+
sed -n '/ERROR/p' file.txt
|
|
272
|
+
|
|
273
|
+
# Insert line before match
|
|
274
|
+
sed '/pattern/i\New line before' file.txt
|
|
275
|
+
|
|
276
|
+
# Append line after match
|
|
277
|
+
sed '/pattern/a\New line after' file.txt
|
|
278
|
+
|
|
279
|
+
# Change entire line
|
|
280
|
+
sed '/pattern/c\Replacement line' file.txt
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### sed Advanced Patterns
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
# Remove comments
|
|
287
|
+
sed 's/#.*//' file.txt
|
|
288
|
+
|
|
289
|
+
# Remove leading whitespace
|
|
290
|
+
sed 's/^[ \t]*//' file.txt
|
|
291
|
+
|
|
292
|
+
# Remove trailing whitespace
|
|
293
|
+
sed 's/[ \t]*$//' file.txt
|
|
294
|
+
|
|
295
|
+
# Remove HTML tags
|
|
296
|
+
sed 's/<[^>]*>//g' file.html
|
|
297
|
+
|
|
298
|
+
# Extract text between delimiters
|
|
299
|
+
sed -n 's/.*<title>\(.*\)<\/title>.*/\1/p' file.html
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Combining Tools: Pipeline Patterns
|
|
303
|
+
|
|
304
|
+
### grep + awk
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
# Filter then extract fields
|
|
308
|
+
grep "ERROR" log.txt | awk '{print $1, $5}'
|
|
309
|
+
|
|
310
|
+
# Filter multiple patterns, process
|
|
311
|
+
grep -E "ERROR|WARN" log.txt | awk '{count[$2]++} END {for (i in count) print i, count[i]}'
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### sed + awk
|
|
315
|
+
|
|
316
|
+
```bash
|
|
317
|
+
# Clean then process
|
|
318
|
+
sed 's/[^[:print:]]//g' data.txt | awk '{sum += $2} END {print sum}'
|
|
319
|
+
|
|
320
|
+
# Remove comments, extract fields
|
|
321
|
+
sed 's/#.*//' config.txt | awk -F'=' '{print $1}'
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### Complete Pipeline Example
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
# Analyze web server logs
|
|
328
|
+
cat access.log \
|
|
329
|
+
| grep "GET" \
|
|
330
|
+
| grep -v "robot" \
|
|
331
|
+
| sed 's/.*HTTP\/[0-9.]*" //' \
|
|
332
|
+
| awk '$1 >= 200 && $1 < 300 {success++} $1 >= 400 {fail++} END {print "Success:", success, "Fail:", fail}'
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## Performance Tips
|
|
336
|
+
|
|
337
|
+
### grep Performance
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
# Use -F for fixed strings (faster than regex)
|
|
341
|
+
grep -F "literal.string" large_file.txt
|
|
342
|
+
|
|
343
|
+
# Use -m to stop after N matches
|
|
344
|
+
grep -m 10 "pattern" large_file.txt
|
|
345
|
+
|
|
346
|
+
# Parallel grep for large files
|
|
347
|
+
parallel -j4 grep "pattern" ::: chunk1 chunk2 chunk3 chunk4
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### awk Performance
|
|
351
|
+
|
|
352
|
+
```bash
|
|
353
|
+
# Exit early if possible
|
|
354
|
+
awk '{if (condition) {print; exit}}' large_file.txt
|
|
355
|
+
|
|
356
|
+
# Process only needed lines
|
|
357
|
+
awk 'NR > 1000 {exit} {process}' large_file.txt
|
|
358
|
+
|
|
359
|
+
# Use built-in functions efficiently
|
|
360
|
+
awk '{count[$1]++} END {for (i in count) print i, count[i]}' file.txt
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### sed Performance
|
|
364
|
+
|
|
365
|
+
```bash
|
|
366
|
+
# Minimize patterns
|
|
367
|
+
sed -e 's/a/b/g' -e 's/c/d/g' file.txt # Better than multiple sed calls
|
|
368
|
+
|
|
369
|
+
# Use in-place editing for large files
|
|
370
|
+
sed -i 's/old/new/g' large_file.txt # Avoids loading entire file
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### Avoid Useless Use of cat
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
# Bad
|
|
377
|
+
cat file.txt | grep "pattern"
|
|
378
|
+
cat file.txt | awk '{print $1}'
|
|
379
|
+
cat file.txt | sed 's/old/new/g'
|
|
380
|
+
|
|
381
|
+
# Good
|
|
382
|
+
grep "pattern" file.txt
|
|
383
|
+
awk '{print $1}' file.txt
|
|
384
|
+
sed 's/old/new/g' file.txt
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
## Real-World Examples
|
|
388
|
+
|
|
389
|
+
### Example 1: CSV Processing
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
# Extract specific columns from CSV
|
|
393
|
+
awk -F',' '{print $1, $3, $5}' data.csv
|
|
394
|
+
|
|
395
|
+
# Filter rows by value
|
|
396
|
+
awk -F',' '$3 > 1000 {print $0}' data.csv
|
|
397
|
+
|
|
398
|
+
# Calculate sum per category
|
|
399
|
+
awk -F',' '{sum[$1] += $3} END {for (cat in sum) print cat, sum[cat]}' sales.csv
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Example 2: Log Analysis
|
|
403
|
+
|
|
404
|
+
```bash
|
|
405
|
+
# Error rate over time
|
|
406
|
+
grep "ERROR" app.log \
|
|
407
|
+
| awk '{print $1}' \
|
|
408
|
+
| uniq -c \
|
|
409
|
+
| awk '{print $2, $1}'
|
|
410
|
+
|
|
411
|
+
# Top 10 error messages
|
|
412
|
+
grep "ERROR" app.log \
|
|
413
|
+
| sed 's/.*ERROR: //' \
|
|
414
|
+
| sort \
|
|
415
|
+
| uniq -c \
|
|
416
|
+
| sort -rn \
|
|
417
|
+
| head -10
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### Example 3: Configuration File Processing
|
|
421
|
+
|
|
422
|
+
```bash
|
|
423
|
+
# Extract non-comment, non-empty lines
|
|
424
|
+
sed -e 's/#.*//' -e '/^$/d' config.txt
|
|
425
|
+
|
|
426
|
+
# Convert KEY=VALUE to JSON
|
|
427
|
+
awk -F'=' 'BEGIN {print "{"} {printf " \"%s\": \"%s\",\n", $1, $2} END {print "}"}' config.txt
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
---
|
|
431
|
+
|
|
432
|
+
## References
|
|
433
|
+
|
|
434
|
+
- [GNU grep Manual](https://www.gnu.org/software/grep/manual/grep.html)
|
|
435
|
+
- [GNU awk Manual](https://www.gnu.org/software/gawk/manual/gawk.html)
|
|
436
|
+
- [GNU sed Manual](https://www.gnu.org/software/sed/manual/sed.html)
|
|
437
|
+
- [The Art of Command Line](https://github.com/jlevy/the-art-of-command-line)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
usage() {
|
|
5
|
+
cat << EOF
|
|
6
|
+
Usage: ${0##*/} [OPTIONS] LOG_FILE
|
|
7
|
+
|
|
8
|
+
Analyze log files and generate summary reports
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
-h Show this help
|
|
12
|
+
-t TYPE Report type: errors|summary (default: summary)
|
|
13
|
+
-o FILE Output file (default: stdout)
|
|
14
|
+
|
|
15
|
+
Examples:
|
|
16
|
+
${0##*/} application.log
|
|
17
|
+
${0##*/} -t errors -o errors.txt application.log
|
|
18
|
+
EOF
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
analyze_errors() {
|
|
22
|
+
local log_file="$1"
|
|
23
|
+
|
|
24
|
+
echo "Error Summary:"
|
|
25
|
+
echo "=============="
|
|
26
|
+
|
|
27
|
+
grep "ERROR" "${log_file}" \
|
|
28
|
+
| sed 's/.*ERROR: //' \
|
|
29
|
+
| sed 's/ -.*//' \
|
|
30
|
+
| sort \
|
|
31
|
+
| uniq -c \
|
|
32
|
+
| sort -rn \
|
|
33
|
+
| awk '{count=$1; $1=""; sub(/^ /,""); printf " %-40s %6d\n", $0, count}'
|
|
34
|
+
|
|
35
|
+
echo ""
|
|
36
|
+
echo "Total errors: $(grep -c "ERROR" "${log_file}" 2>/dev/null || true)"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
generate_summary() {
|
|
40
|
+
local log_file="$1"
|
|
41
|
+
|
|
42
|
+
echo "Log File Analysis Summary"
|
|
43
|
+
echo "========================="
|
|
44
|
+
echo ""
|
|
45
|
+
echo "File: ${log_file}"
|
|
46
|
+
echo "Total lines: $(wc -l < "${log_file}")"
|
|
47
|
+
echo ""
|
|
48
|
+
|
|
49
|
+
echo "Log Levels:"
|
|
50
|
+
for level in DEBUG INFO WARN ERROR FATAL; do
|
|
51
|
+
local count
|
|
52
|
+
count=$(grep -c "${level}" "${log_file}" 2>/dev/null || true)
|
|
53
|
+
printf " %-10s %6d\n" "${level}:" "${count}"
|
|
54
|
+
done
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
main() {
|
|
58
|
+
local report_type="summary"
|
|
59
|
+
local output_file=""
|
|
60
|
+
local log_file=""
|
|
61
|
+
|
|
62
|
+
while getopts ":ht:o:" opt; do
|
|
63
|
+
case ${opt} in
|
|
64
|
+
h) usage; exit 0 ;;
|
|
65
|
+
t) report_type="${OPTARG}" ;;
|
|
66
|
+
o) output_file="${OPTARG}" ;;
|
|
67
|
+
:) echo "Option -${OPTARG} requires an argument" >&2; exit 1 ;;
|
|
68
|
+
\?) echo "Invalid option: -${OPTARG}" >&2; exit 1 ;;
|
|
69
|
+
esac
|
|
70
|
+
done
|
|
71
|
+
shift $((OPTIND - 1))
|
|
72
|
+
|
|
73
|
+
log_file="${1:-}"
|
|
74
|
+
[[ -n "${log_file}" ]] || { echo "Error: LOG_FILE required" >&2; usage; exit 1; }
|
|
75
|
+
[[ -f "${log_file}" ]] || { echo "Error: File not found: ${log_file}" >&2; exit 1; }
|
|
76
|
+
|
|
77
|
+
local output
|
|
78
|
+
case "${report_type}" in
|
|
79
|
+
errors) output=$(analyze_errors "${log_file}") ;;
|
|
80
|
+
summary) output=$(generate_summary "${log_file}") ;;
|
|
81
|
+
*) echo "Invalid report type: ${report_type}" >&2; exit 1 ;;
|
|
82
|
+
esac
|
|
83
|
+
|
|
84
|
+
if [[ -n "${output_file}" ]]; then
|
|
85
|
+
echo "${output}" > "${output_file}"
|
|
86
|
+
echo "Report saved to: ${output_file}"
|
|
87
|
+
else
|
|
88
|
+
echo "${output}"
|
|
89
|
+
fi
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
main "$@"
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Generate bash script templates
|
|
4
|
+
#
|
|
5
|
+
|
|
6
|
+
set -euo pipefail
|
|
7
|
+
|
|
8
|
+
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
9
|
+
readonly TEMPLATES_DIR="${SCRIPT_DIR}/../assets/templates"
|
|
10
|
+
|
|
11
|
+
list_templates() {
|
|
12
|
+
local found=false
|
|
13
|
+
|
|
14
|
+
for template_path in "${TEMPLATES_DIR}"/*-template.sh; do
|
|
15
|
+
if [[ -f "${template_path}" ]]; then
|
|
16
|
+
found=true
|
|
17
|
+
basename "${template_path}" | sed 's/-template.sh$//'
|
|
18
|
+
fi
|
|
19
|
+
done
|
|
20
|
+
|
|
21
|
+
if [[ "${found}" == "false" ]]; then
|
|
22
|
+
echo "(none)"
|
|
23
|
+
fi
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
default_output_file() {
|
|
27
|
+
local template_type="$1"
|
|
28
|
+
echo "./${template_type}-script.sh"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
validate_template_type() {
|
|
32
|
+
local value="$1"
|
|
33
|
+
|
|
34
|
+
# Block traversal payloads and path separators explicitly.
|
|
35
|
+
if [[ "$value" == *"/"* || "$value" == *".."* ]]; then
|
|
36
|
+
echo "Error: Invalid TEMPLATE_TYPE: ${value}" >&2
|
|
37
|
+
echo "Allowed characters: letters, digits, '_' and '-'" >&2
|
|
38
|
+
return 1
|
|
39
|
+
fi
|
|
40
|
+
|
|
41
|
+
if [[ ! "$value" =~ ^[a-zA-Z0-9_-]+$ ]]; then
|
|
42
|
+
echo "Error: Invalid TEMPLATE_TYPE: ${value}" >&2
|
|
43
|
+
echo "Allowed characters: letters, digits, '_' and '-'" >&2
|
|
44
|
+
return 1
|
|
45
|
+
fi
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
usage() {
|
|
49
|
+
cat << EOF
|
|
50
|
+
Usage: ${0##*/} TEMPLATE_TYPE [OUTPUT_FILE]
|
|
51
|
+
|
|
52
|
+
Generate a bash script from a template.
|
|
53
|
+
|
|
54
|
+
Templates include:
|
|
55
|
+
- Proper shebang and strict mode (set -euo pipefail)
|
|
56
|
+
- Logging functions (debug, info, warn, error)
|
|
57
|
+
- Error handling (die, check_command, validate_file)
|
|
58
|
+
- Argument parsing with getopts
|
|
59
|
+
- Cleanup trap handlers
|
|
60
|
+
- Usage documentation
|
|
61
|
+
|
|
62
|
+
Examples:
|
|
63
|
+
${0##*/} standard
|
|
64
|
+
${0##*/} standard myscript.sh
|
|
65
|
+
${0##*/} standard /usr/local/bin/deploy.sh
|
|
66
|
+
|
|
67
|
+
Available templates:
|
|
68
|
+
$(list_templates)
|
|
69
|
+
|
|
70
|
+
EOF
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
main() {
|
|
74
|
+
local template_type
|
|
75
|
+
local output_file
|
|
76
|
+
local template_file
|
|
77
|
+
local output_dir
|
|
78
|
+
|
|
79
|
+
if [[ $# -eq 0 ]]; then
|
|
80
|
+
echo "Error: TEMPLATE_TYPE is required" >&2
|
|
81
|
+
usage
|
|
82
|
+
exit 1
|
|
83
|
+
fi
|
|
84
|
+
|
|
85
|
+
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
|
86
|
+
usage
|
|
87
|
+
exit 0
|
|
88
|
+
fi
|
|
89
|
+
|
|
90
|
+
if [[ $# -gt 2 ]]; then
|
|
91
|
+
echo "Error: Too many arguments" >&2
|
|
92
|
+
usage
|
|
93
|
+
exit 1
|
|
94
|
+
fi
|
|
95
|
+
|
|
96
|
+
template_type="$1"
|
|
97
|
+
validate_template_type "$template_type" || exit 1
|
|
98
|
+
output_file="${2:-$(default_output_file "${template_type}")}"
|
|
99
|
+
template_file="${TEMPLATES_DIR}/${template_type}-template.sh"
|
|
100
|
+
|
|
101
|
+
if [[ ! -f "${template_file}" ]]; then
|
|
102
|
+
echo "Error: Template not found: ${template_type}" >&2
|
|
103
|
+
echo "Available templates:" >&2
|
|
104
|
+
while IFS= read -r template_name; do
|
|
105
|
+
echo " ${template_name}" >&2
|
|
106
|
+
done < <(list_templates)
|
|
107
|
+
exit 1
|
|
108
|
+
fi
|
|
109
|
+
|
|
110
|
+
output_dir="$(dirname "${output_file}")"
|
|
111
|
+
if [[ "${output_dir}" != "." ]]; then
|
|
112
|
+
mkdir -p "${output_dir}"
|
|
113
|
+
fi
|
|
114
|
+
|
|
115
|
+
cp "${template_file}" "${output_file}"
|
|
116
|
+
chmod u+x "${output_file}"
|
|
117
|
+
|
|
118
|
+
echo "Created script: ${output_file}"
|
|
119
|
+
echo "Template: ${template_type}"
|
|
120
|
+
echo "Source: ${template_file}"
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
main "$@"
|