sed_edit
Modify files using sed patterns for single-line changes, replacements, and text edits. Create backups or preview changes before saving to ensure accuracy.
Instructions
Make small edits to files using sed patterns. Efficient for single-line changes, pattern replacements, and simple text transformations.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| backup | No | Create backup file before editing | |
| file | Yes | Path to the file to edit | |
| pattern | Yes | Sed pattern (e.g., "s/old/new/g" for substitution) | |
| preview | No | Preview changes without modifying file |
Implementation Reference
- src/index.ts:567-606 (handler)The main handler for the sed_edit tool. It destructures the input arguments, checks if the file exists, constructs a Perl command (using sed-like pattern) for editing or previewing, executes it via execAsync, handles errors, and returns a success or preview message.case 'sed_edit': { const { file, pattern, backup = true, preview = false } = args; // Check if file exists if (!existsSync(file)) { throw new Error(`File not found: ${file}`); } // Build sed command - use perl for better compatibility let sedCmd; if (preview) { // For preview, create a temp copy and diff const tempFile = `${file}.preview.tmp`; await execAsync(`rm -f .bak; cp '${file}' '${tempFile}'`); // Apply change to temp file sedCmd = `perl -i -pe '${pattern}' '${tempFile}' && diff -u '${file}' '${tempFile}' | head -50; rm -f '${tempFile}'`; } else { // Use perl for actual edits (more portable than sed) const backupExt = backup ? '.bak' : ''; sedCmd = `perl -i${backupExt} -pe '${pattern}' '${file}'`; } const { stdout, stderr } = await execAsync(sedCmd); if (stderr) { throw new Error(`Sed error: ${stderr}`); } return { content: [ { type: 'text', text: preview ? `Preview of changes:\n${stdout || 'No changes would be made'}` : `Successfully edited ${file}${backup ? ' (backup created as .bak)' : ''}` } ] }; }
- src/index.ts:39-66 (registration)Registers the sed_edit tool in the listTools response, including its name, description, and input schema.{ name: 'sed_edit', description: 'Make small edits to files using sed patterns. Efficient for single-line changes, pattern replacements, and simple text transformations.', inputSchema: { type: 'object', properties: { file: { type: 'string', description: 'Path to the file to edit' }, pattern: { type: 'string', description: 'Sed pattern (e.g., "s/old/new/g" for substitution)' }, backup: { type: 'boolean', default: true, description: 'Create backup file before editing' }, preview: { type: 'boolean', default: false, description: 'Preview changes without modifying file' } }, required: ['file', 'pattern'] } },
- src/index.ts:42-65 (schema)Defines the input schema for the sed_edit tool, specifying properties like file path, sed pattern, backup, and preview options.inputSchema: { type: 'object', properties: { file: { type: 'string', description: 'Path to the file to edit' }, pattern: { type: 'string', description: 'Sed pattern (e.g., "s/old/new/g" for substitution)' }, backup: { type: 'boolean', default: true, description: 'Create backup file before editing' }, preview: { type: 'boolean', default: false, description: 'Preview changes without modifying file' } }, required: ['file', 'pattern'] }