@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,454 @@
|
|
|
1
|
+
# GNU sed Reference Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
sed (Stream EDitor) is a powerful text processing tool that performs basic text transformations on an input stream (file or pipeline).
|
|
6
|
+
|
|
7
|
+
**Official Manual:** https://www.gnu.org/software/sed/manual/
|
|
8
|
+
**Man Page:** `man sed`
|
|
9
|
+
|
|
10
|
+
## Basic Syntax
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
sed [OPTIONS] 'command' file
|
|
14
|
+
sed [OPTIONS] -e 'command1' -e 'command2' file
|
|
15
|
+
sed [OPTIONS] -f script.sed file
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Common Options
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
-n, --quiet, --silent # Suppress automatic output
|
|
22
|
+
-e SCRIPT # Add script to commands
|
|
23
|
+
-f FILE # Add contents of FILE as commands
|
|
24
|
+
-i[SUFFIX] # Edit files in-place
|
|
25
|
+
-r, -E # Use extended regular expressions
|
|
26
|
+
--debug # Annotate program execution
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Basic Commands
|
|
30
|
+
|
|
31
|
+
### Substitution (s)
|
|
32
|
+
```bash
|
|
33
|
+
# Basic substitution
|
|
34
|
+
sed 's/old/new/' file # Replace first occurrence
|
|
35
|
+
sed 's/old/new/g' file # Replace all occurrences
|
|
36
|
+
sed 's/old/new/2' file # Replace second occurrence
|
|
37
|
+
sed 's/old/new/gi' file # Case-insensitive, all
|
|
38
|
+
|
|
39
|
+
# With different delimiter
|
|
40
|
+
sed 's|/old/path|/new/path|g' file
|
|
41
|
+
sed 's#old#new#g' file
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Deletion (d)
|
|
45
|
+
```bash
|
|
46
|
+
# Delete lines
|
|
47
|
+
sed '5d' file # Delete line 5
|
|
48
|
+
sed '5,10d' file # Delete lines 5-10
|
|
49
|
+
sed '/pattern/d' file # Delete matching lines
|
|
50
|
+
sed '/^$/d' file # Delete empty lines
|
|
51
|
+
sed '/^#/d' file # Delete comment lines
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Print (p)
|
|
55
|
+
```bash
|
|
56
|
+
# Print lines
|
|
57
|
+
sed -n '5p' file # Print only line 5
|
|
58
|
+
sed -n '5,10p' file # Print lines 5-10
|
|
59
|
+
sed -n '/pattern/p' file # Print matching lines
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Append (a), Insert (i), Change (c)
|
|
63
|
+
```bash
|
|
64
|
+
# Append after line
|
|
65
|
+
sed '5a\New line' file
|
|
66
|
+
|
|
67
|
+
# Insert before line
|
|
68
|
+
sed '5i\New line' file
|
|
69
|
+
|
|
70
|
+
# Change line
|
|
71
|
+
sed '5c\Replacement line' file
|
|
72
|
+
|
|
73
|
+
# With pattern
|
|
74
|
+
sed '/pattern/a\New line after match' file
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Address Ranges
|
|
78
|
+
|
|
79
|
+
### Line Numbers
|
|
80
|
+
```bash
|
|
81
|
+
sed '5s/old/new/' file # Line 5 only
|
|
82
|
+
sed '5,10s/old/new/' file # Lines 5-10
|
|
83
|
+
sed '5,$s/old/new/' file # Line 5 to end
|
|
84
|
+
sed '1,5d' file # Delete first 5 lines
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Patterns
|
|
88
|
+
```bash
|
|
89
|
+
sed '/start/,/end/d' file # Delete from start to end pattern
|
|
90
|
+
sed '/pattern/s/old/new/' file # Substitute in matching lines
|
|
91
|
+
sed '1,/pattern/d' file # Delete from line 1 to first match
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Special Addresses
|
|
95
|
+
```bash
|
|
96
|
+
sed '$d' file # Delete last line
|
|
97
|
+
sed '1d' file # Delete first line
|
|
98
|
+
sed '$s/old/new/' file # Substitute in last line
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Advanced Substitution
|
|
102
|
+
|
|
103
|
+
### Backreferences
|
|
104
|
+
```bash
|
|
105
|
+
# Capture and reuse
|
|
106
|
+
sed 's/\([0-9]\+\)/Number: \1/' file
|
|
107
|
+
|
|
108
|
+
# Multiple captures
|
|
109
|
+
sed 's/\([a-z]\+\) \([0-9]\+\)/\2 \1/' file
|
|
110
|
+
|
|
111
|
+
# With ERE (-E or -r)
|
|
112
|
+
sed -E 's/([0-9]+)/Number: \1/' file
|
|
113
|
+
sed -E 's/([a-z]+) ([0-9]+)/\2 \1/' file
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Flags
|
|
117
|
+
```bash
|
|
118
|
+
s/old/new/ # Replace first
|
|
119
|
+
s/old/new/g # Replace all (global)
|
|
120
|
+
s/old/new/2 # Replace 2nd occurrence
|
|
121
|
+
s/old/new/i # Case-insensitive
|
|
122
|
+
s/old/new/I # Case-insensitive (same as i)
|
|
123
|
+
s/old/new/p # Print if substitution made
|
|
124
|
+
s/old/new/w file # Write if substitution made
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Special Characters in Replacement
|
|
128
|
+
```bash
|
|
129
|
+
& # Matched string
|
|
130
|
+
\1, \2, etc # Backreferences
|
|
131
|
+
\L, \U # Convert to lower/upper (GNU sed)
|
|
132
|
+
\n # Newline (in replacement)
|
|
133
|
+
\\ # Literal backslash
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Multiple Commands
|
|
137
|
+
|
|
138
|
+
### Multiple -e Options
|
|
139
|
+
```bash
|
|
140
|
+
sed -e 's/old/new/g' -e 's/foo/bar/g' file
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Semicolon Separator
|
|
144
|
+
```bash
|
|
145
|
+
sed 's/old/new/g; s/foo/bar/g' file
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Multi-line Script
|
|
149
|
+
```bash
|
|
150
|
+
sed '
|
|
151
|
+
s/old/new/g
|
|
152
|
+
s/foo/bar/g
|
|
153
|
+
/pattern/d
|
|
154
|
+
' file
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## In-place Editing
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
# Edit file in-place
|
|
161
|
+
sed -i 's/old/new/g' file
|
|
162
|
+
|
|
163
|
+
# Create backup
|
|
164
|
+
sed -i.bak 's/old/new/g' file
|
|
165
|
+
|
|
166
|
+
# Multiple files
|
|
167
|
+
sed -i 's/old/new/g' *.txt
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Pattern Matching
|
|
171
|
+
|
|
172
|
+
### BRE (Basic Regular Expressions) - Default
|
|
173
|
+
```bash
|
|
174
|
+
sed 's/^/#/' file # Add # at beginning
|
|
175
|
+
sed 's/$/;/' file # Add ; at end
|
|
176
|
+
sed 's/[0-9]\+/X/g' file # Replace numbers (BRE)
|
|
177
|
+
sed 's/\<word\>/WORD/g' file # Word boundaries (BRE)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### ERE (Extended Regular Expressions)
|
|
181
|
+
```bash
|
|
182
|
+
sed -E 's/[0-9]+/X/g' file # Replace numbers (ERE)
|
|
183
|
+
sed -E 's/(foo|bar)/baz/g' file # Alternation
|
|
184
|
+
sed -E 's/\s+/ /g' file # Multiple spaces to one
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Practical Examples for Shell Scripts
|
|
188
|
+
|
|
189
|
+
### Configuration File Editing
|
|
190
|
+
```bash
|
|
191
|
+
# Change a config value
|
|
192
|
+
sed -i 's/^Port .*/Port 2222/' /etc/ssh/sshd_config
|
|
193
|
+
|
|
194
|
+
# Uncomment a line
|
|
195
|
+
sed -i 's/^#\(.*option.*\)/\1/' config.file
|
|
196
|
+
|
|
197
|
+
# Comment out a line
|
|
198
|
+
sed -i 's/^\(.*dangerous.*\)/#\1/' config.file
|
|
199
|
+
|
|
200
|
+
# Add line after pattern
|
|
201
|
+
sed -i '/\[section\]/a new_setting = value' config.ini
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Text Processing
|
|
205
|
+
```bash
|
|
206
|
+
# Remove trailing whitespace
|
|
207
|
+
sed 's/[[:space:]]*$//' file
|
|
208
|
+
|
|
209
|
+
# Remove leading whitespace
|
|
210
|
+
sed 's/^[[:space:]]*//' file
|
|
211
|
+
|
|
212
|
+
# Remove empty lines
|
|
213
|
+
sed '/^$/d' file
|
|
214
|
+
|
|
215
|
+
# Remove comments and empty lines
|
|
216
|
+
sed '/^#/d; /^$/d' file
|
|
217
|
+
|
|
218
|
+
# Double-space file
|
|
219
|
+
sed 'G' file
|
|
220
|
+
|
|
221
|
+
# Remove duplicate lines (consecutive)
|
|
222
|
+
sed '$!N; /^\(.*\)\n\1$/!P; D' file
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Path Manipulation
|
|
226
|
+
```bash
|
|
227
|
+
# Change paths
|
|
228
|
+
sed 's|/old/path|/new/path|g' file
|
|
229
|
+
|
|
230
|
+
# Extract filename from path
|
|
231
|
+
echo "/path/to/file.txt" | sed 's|.*/||'
|
|
232
|
+
|
|
233
|
+
# Extract directory from path
|
|
234
|
+
echo "/path/to/file.txt" | sed 's|/[^/]*$||'
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Log File Processing
|
|
238
|
+
```bash
|
|
239
|
+
# Extract IP addresses
|
|
240
|
+
sed -n 's/.*\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p' log
|
|
241
|
+
|
|
242
|
+
# Filter by date
|
|
243
|
+
sed -n '/2025-01-01/,/2025-01-31/p' log
|
|
244
|
+
|
|
245
|
+
# Remove timestamp
|
|
246
|
+
sed 's/^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} //' log
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Code Refactoring
|
|
250
|
+
```bash
|
|
251
|
+
# Rename function
|
|
252
|
+
sed -i 's/\boldFunctionName\b/newFunctionName/g' *.sh
|
|
253
|
+
|
|
254
|
+
# Change variable
|
|
255
|
+
sed -i 's/\$old_var/\$new_var/g' script.sh
|
|
256
|
+
|
|
257
|
+
# Update shebang
|
|
258
|
+
sed -i '1s|^#!/bin/sh|#!/bin/bash|' *.sh
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Advanced Features
|
|
262
|
+
|
|
263
|
+
### Hold Space
|
|
264
|
+
```bash
|
|
265
|
+
# Hold space commands
|
|
266
|
+
h # Copy pattern space to hold space
|
|
267
|
+
H # Append pattern space to hold space
|
|
268
|
+
g # Copy hold space to pattern space
|
|
269
|
+
G # Append hold space to pattern space
|
|
270
|
+
x # Exchange pattern and hold spaces
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Hold Space Examples
|
|
274
|
+
```bash
|
|
275
|
+
# Reverse file
|
|
276
|
+
sed '1!G;h;$!d' file
|
|
277
|
+
|
|
278
|
+
# Print last line
|
|
279
|
+
sed -n '$p' file
|
|
280
|
+
sed -n 'h;$p' file
|
|
281
|
+
|
|
282
|
+
# Append next line to current
|
|
283
|
+
sed 'N;s/\n/ /' file
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Branching
|
|
287
|
+
```bash
|
|
288
|
+
# Label and branch
|
|
289
|
+
:label # Define label
|
|
290
|
+
b label # Branch to label
|
|
291
|
+
t label # Branch if substitution succeeded
|
|
292
|
+
T label # Branch if substitution failed
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Branching Examples
|
|
296
|
+
```bash
|
|
297
|
+
# Remove C-style comments
|
|
298
|
+
sed -n '
|
|
299
|
+
/\/\*/ {
|
|
300
|
+
:loop
|
|
301
|
+
/\*\// {
|
|
302
|
+
s|/\*.*\*/||g
|
|
303
|
+
p
|
|
304
|
+
b
|
|
305
|
+
}
|
|
306
|
+
N
|
|
307
|
+
b loop
|
|
308
|
+
}
|
|
309
|
+
p
|
|
310
|
+
' file
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
## Common Patterns
|
|
314
|
+
|
|
315
|
+
### Join Lines
|
|
316
|
+
```bash
|
|
317
|
+
# Join all lines
|
|
318
|
+
sed ':a;N;$!ba;s/\n/ /g' file
|
|
319
|
+
|
|
320
|
+
# Join lines ending with backslash
|
|
321
|
+
sed -e :a -e '/\\$/N; s/\\\n//; ta' file
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### Number Lines
|
|
325
|
+
```bash
|
|
326
|
+
sed = file | sed 'N;s/\n/\t/'
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Reverse Lines
|
|
330
|
+
```bash
|
|
331
|
+
sed '1!G;h;$!d' file
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Print Specific Lines
|
|
335
|
+
```bash
|
|
336
|
+
# Print line 5
|
|
337
|
+
sed -n '5p' file
|
|
338
|
+
|
|
339
|
+
# Print first 10 lines
|
|
340
|
+
sed -n '1,10p' file
|
|
341
|
+
|
|
342
|
+
# Print last 10 lines
|
|
343
|
+
sed -n -e :a -e '1,10!{P;N;D;};N;ba' file
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## Common Pitfalls in Shell Scripts
|
|
347
|
+
|
|
348
|
+
### 1. Special Characters in Pattern
|
|
349
|
+
```bash
|
|
350
|
+
# Wrong - . matches any character
|
|
351
|
+
sed 's/192.168.1.1/new/' file
|
|
352
|
+
|
|
353
|
+
# Right - escape dots
|
|
354
|
+
sed 's/192\.168\.1\.1/new/' file
|
|
355
|
+
|
|
356
|
+
# Or use different delimiter
|
|
357
|
+
sed 's|192.168.1.1|new|' file
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### 2. Not Escaping Backreferences in BRE
|
|
361
|
+
```bash
|
|
362
|
+
# Wrong (BRE)
|
|
363
|
+
sed 's/([0-9]+)/\1/' file
|
|
364
|
+
|
|
365
|
+
# Right (BRE)
|
|
366
|
+
sed 's/\([0-9]\+\)/\1/' file
|
|
367
|
+
|
|
368
|
+
# Right (ERE)
|
|
369
|
+
sed -E 's/([0-9]+)/\1/' file
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### 3. In-place Editing Without Backup
|
|
373
|
+
```bash
|
|
374
|
+
# Dangerous
|
|
375
|
+
sed -i 's/old/new/' important_file
|
|
376
|
+
|
|
377
|
+
# Safer
|
|
378
|
+
sed -i.backup 's/old/new/' important_file
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### 4. Using sed for Line Counting
|
|
382
|
+
```bash
|
|
383
|
+
# Inefficient
|
|
384
|
+
sed -n '$=' file
|
|
385
|
+
|
|
386
|
+
# Better
|
|
387
|
+
wc -l < file
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### 5. Not Quoting Variables Properly
|
|
391
|
+
```bash
|
|
392
|
+
# Wrong
|
|
393
|
+
sed "s/$old/$new/g" file # Dangerous if vars contain /
|
|
394
|
+
|
|
395
|
+
# Better
|
|
396
|
+
old_escaped=$(printf '%s\n' "$old" | sed 's:[\\/&]:\\&:g')
|
|
397
|
+
new_escaped=$(printf '%s\n' "$new" | sed 's:[\\/&]:\\&:g')
|
|
398
|
+
sed "s/$old_escaped/$new_escaped/g" file
|
|
399
|
+
|
|
400
|
+
# Or use different delimiter
|
|
401
|
+
sed "s|$old|$new|g" file
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## Performance Tips
|
|
405
|
+
|
|
406
|
+
### 1. Use Appropriate Tools
|
|
407
|
+
```bash
|
|
408
|
+
# For simple replacements, consider using other tools
|
|
409
|
+
# sed is great for complex patterns, but for simple tasks:
|
|
410
|
+
|
|
411
|
+
# Instead of
|
|
412
|
+
sed 's/old/new/g' file
|
|
413
|
+
|
|
414
|
+
# Consider
|
|
415
|
+
tr 'old' 'new' < file # For single-character replacement
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### 2. Minimize Pattern Matching
|
|
419
|
+
```bash
|
|
420
|
+
# Less efficient
|
|
421
|
+
sed '/pattern/s/old/new/g' large_file
|
|
422
|
+
|
|
423
|
+
# More efficient if pattern is rare
|
|
424
|
+
grep 'pattern' large_file | sed 's/old/new/g'
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### 3. Combine Commands
|
|
428
|
+
```bash
|
|
429
|
+
# Less efficient
|
|
430
|
+
sed 's/old/new/g' file | sed 's/foo/bar/g'
|
|
431
|
+
|
|
432
|
+
# More efficient
|
|
433
|
+
sed 's/old/new/g; s/foo/bar/g' file
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
## Testing sed Commands
|
|
437
|
+
|
|
438
|
+
```bash
|
|
439
|
+
# Test before in-place edit
|
|
440
|
+
sed 's/old/new/g' file | head
|
|
441
|
+
|
|
442
|
+
# Show only changes
|
|
443
|
+
sed -n 's/old/new/gp' file
|
|
444
|
+
|
|
445
|
+
# Count changes
|
|
446
|
+
sed -n 's/old/new/gp' file | wc -l
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Resources
|
|
450
|
+
|
|
451
|
+
- [GNU sed Manual](https://www.gnu.org/software/sed/manual/)
|
|
452
|
+
- [sed(1) Man Page](https://man7.org/linux/man-pages/man1/sed.1.html)
|
|
453
|
+
- [sed One-Liners](http://sed.sourceforge.net/sed1line.txt)
|
|
454
|
+
- [Grymoire sed Tutorial](https://www.grymoire.com/Unix/Sed.html)
|