diff --git a/src/tools/sed-command-generator/index.ts b/src/tools/sed-command-generator/index.ts new file mode 100644 index 00000000..fead9b81 --- /dev/null +++ b/src/tools/sed-command-generator/index.ts @@ -0,0 +1,13 @@ +import { CursorText } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Sed Command generator', + path: '/sed-command-generator', + description: 'Generate unix sed commands', + keywords: ['sed', 'command', 'generator'], + component: () => import('./sed-command-generator.vue'), + icon: CursorText, + createdAt: new Date('2025-08-17'), + category: 'Text', +}); diff --git a/src/tools/sed-command-generator/sed-command-generator.vue b/src/tools/sed-command-generator/sed-command-generator.vue new file mode 100644 index 00000000..3cb3de88 --- /dev/null +++ b/src/tools/sed-command-generator/sed-command-generator.vue @@ -0,0 +1,140 @@ + + + diff --git a/src/tools/sed-memo/index.ts b/src/tools/sed-memo/index.ts new file mode 100644 index 00000000..adcd4814 --- /dev/null +++ b/src/tools/sed-memo/index.ts @@ -0,0 +1,13 @@ +import { FileText } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Sed cheatsheet', + path: '/sed-memo', + description: 'sed unix command cheatsheet', + keywords: ['sed', 'memo', 'cheatsheet'], + component: () => import('./sed-memo.vue'), + icon: FileText, + createdAt: new Date('2025-08-17'), + category: 'Cheatsheets', +}); diff --git a/src/tools/sed-memo/sed-memo.md b/src/tools/sed-memo/sed-memo.md new file mode 100644 index 00000000..56f0e992 --- /dev/null +++ b/src/tools/sed-memo/sed-memo.md @@ -0,0 +1,211 @@ +# ๐Ÿงพ `sed` Command Cheatsheet (Stream Editor) + +`sed` is a powerful Unix utility for parsing and transforming text using **stream editing**. + +## ๐Ÿ“Œ Basic Syntax + +```bash +sed [OPTIONS] 'script' [file...] +``` + +- `'script'`: One or more editing commands. +- `[file]`: Input file(s). If omitted, `sed` reads from standard input. + +## โš™๏ธ Common Options + +| Option | Description | +|--------|-------------| +| `-n` | Suppress automatic printing of pattern space. Use `p` to print explicitly. | +| `-e` | Add multiple editing commands. | +| `-i` | Edit files in-place (with optional backup: `-i.bak`). | +| `-f` | Read commands from a file. | + +## โœ‚๏ธ Basic Commands + +| Command | Description | +|---------|-------------| +| `p` | Print the current pattern space. | +| `d` | Delete the current pattern space. | +| `s` | Substitute text using regex. | +| `q` | Quit after processing first match. | +| `a` | Append text after current line. | +| `i` | Insert text before current line. | +| `c` | Replace line with new text. | +| `y` | Translate characters (like `tr`). | + +## ๐Ÿ” Substitution Syntax + +```bash +sed 's/pattern/replacement/flags' file +``` + +### Flags + +| Flag | Description | +|------|-------------| +| `g` | Global replacement (all matches in line). | +| `i` | Case-insensitive matching. | +| `p` | Print the line if substitution occurred. | +| `n` | Replace nth occurrence only. | + +### Examples + +```bash +sed 's/foo/bar/' file # Replace first 'foo' with 'bar' +sed 's/foo/bar/g' file # Replace all 'foo' with 'bar' +sed 's/foo/bar/2' file # Replace second 'foo' only +sed 's/foo/bar/ip' file # Case-insensitive + print +``` + +## ๐Ÿ—‘๏ธ Deletion with `sed` + +`sed` can delete lines based on line numbers, patterns, or ranges. + +### ๐Ÿ”ข Delete by Line Number + +```bash +sed '2d' file # Delete line 2 +sed '5,10d' file # Delete lines 5 through 10 +``` + +### ๐Ÿ” Delete by Pattern + +```bash +sed '/error/d' file # Delete lines containing 'error' +sed '/^$/d' file # Delete blank lines +sed '/^#/d' file # Delete comment lines (starting with #) +``` + +### ๐Ÿงฎ Delete by Range and Pattern + +```bash +sed '1,/pattern/d' file # Delete from line 1 to first match of 'pattern' +sed '/start/,/end/d' file # Delete lines between 'start' and 'end' (inclusive) +``` + +### ๐Ÿง  Conditional Deletion + +```bash +sed -n '/pattern/!p' file # Print only lines NOT matching 'pattern' +``` + +### ๐Ÿงน Delete Last Line + +```bash +sed '$d' file # Delete the last line +``` + +## ๐Ÿ“ Addressing Lines + +### Line Numbers + +```bash +sed '2d' file # Delete line 2 +sed '3,5p' file # Print lines 3 to 5 +``` + +### Patterns + +```bash +sed '/error/d' file # Delete lines containing 'error' +sed '/^#/d' file # Delete comment lines +``` + +### Combined + +```bash +sed '1,/pattern/d' file # Delete from line 1 to first match of 'pattern' +``` + +## ๐Ÿงช Advanced Substitution + +### Using Capture Groups + +```bash +sed 's/\(foo\)bar/\1baz/' file +``` + +- `\(...\)` captures a group. +- `\1`, `\2`, etc., refer to captured groups. + +### Escaping Special Characters + +```bash +sed 's/\/usr\/bin/\/usr\/local\/bin/' file +``` + +Or use alternate delimiter: + +```bash +sed 's|/usr/bin|/usr/local/bin|' file +``` + +## ๐Ÿงจ In-Place Editing + +```bash +sed -i 's/foo/bar/g' file # Edit file directly +sed -i.bak 's/foo/bar/g' file # Backup original as file.bak +``` + +## ๐Ÿ“‚ Multiple Commands + +### Inline + +```bash +sed -e 's/foo/bar/' -e '/baz/d' file +``` + +### Block Syntax + +```bash +sed ' +s/foo/bar/ +s/baz/qux/ +' file +``` + +## ๐Ÿงต Append, Insert, Change + +```bash +sed '/pattern/a\Text to append' file +sed '/pattern/i\Text to insert' file +sed '/pattern/c\New line content' file +``` + +## ๐Ÿ”„ Translate Characters + +```bash +sed 'y/abc/ABC/' file # aโ†’A, bโ†’B, cโ†’C +``` + +## ๐Ÿง  Useful Recipes + +### Remove Blank Lines + +```bash +sed '/^$/d' file +``` + +### Remove Leading/Trailing Whitespace + +```bash +sed 's/^[ \t]*//' file # Remove leading +sed 's/[ \t]*$//' file # Remove trailing +``` + +### Replace Tabs with Spaces + +```bash +sed 's/\t/ /g' file +``` + +### Number Lines + +```bash +sed = file | sed 'N;s/\n/\t/' +``` + +## ๐Ÿ“š Resources + +- `man sed` +- GNU sed manual: [https://www.gnu.org/software/sed/manual/sed.html](https://www.gnu.org/software/sed/manual/sed.html) \ No newline at end of file diff --git a/src/tools/sed-memo/sed-memo.vue b/src/tools/sed-memo/sed-memo.vue new file mode 100644 index 00000000..1fda8068 --- /dev/null +++ b/src/tools/sed-memo/sed-memo.vue @@ -0,0 +1,25 @@ + + + + +