mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
1 parent
ae0ef63a03
commit
a3b75a60e7
5 files changed
+402
No files matched your search
@@ -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',
|
||||
});
|
||||
@@ -0,0 +1,140 @@
|
||||
<script setup lang="ts">
|
||||
const form = ref({
|
||||
commandType: 'substitute', // substitute | delete | extract | number
|
||||
pattern: '',
|
||||
replacement: '',
|
||||
delimiter: '/',
|
||||
global: true,
|
||||
caseInsensitive: false,
|
||||
inPlace: false,
|
||||
extendedRegex: false,
|
||||
suppressPrint: false,
|
||||
targetFile: '',
|
||||
});
|
||||
|
||||
const sedCommand = computed(() => {
|
||||
const {
|
||||
commandType,
|
||||
pattern,
|
||||
replacement,
|
||||
delimiter,
|
||||
global,
|
||||
caseInsensitive,
|
||||
inPlace,
|
||||
extendedRegex,
|
||||
suppressPrint,
|
||||
targetFile,
|
||||
} = form.value;
|
||||
|
||||
const options = [
|
||||
inPlace ? '-i' : '',
|
||||
extendedRegex ? '-E' : '',
|
||||
suppressPrint ? '-n' : '',
|
||||
].filter(Boolean).join(' ');
|
||||
|
||||
let expr = '';
|
||||
|
||||
if (commandType === 'substitute') {
|
||||
if (!pattern || !replacement) {
|
||||
return '';
|
||||
}
|
||||
const flags = `${global ? 'g' : ''}${caseInsensitive ? 'i' : ''}`;
|
||||
expr = `s${delimiter}${pattern}${delimiter}${replacement}${delimiter}${flags}`;
|
||||
}
|
||||
else if (commandType === 'delete') {
|
||||
if (!pattern) {
|
||||
return '';
|
||||
}
|
||||
expr = `/${pattern}/d`;
|
||||
}
|
||||
else if (commandType === 'extract') {
|
||||
if (!pattern) {
|
||||
return '';
|
||||
}
|
||||
expr = `/${pattern}/p`;
|
||||
}
|
||||
else if (commandType === 'number') {
|
||||
expr = '=';
|
||||
}
|
||||
|
||||
const file = targetFile || 'yourfile.txt';
|
||||
return `sed ${options} '${expr}' ${file}`.trim();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<n-space justify="center" mb-1 mt-1>
|
||||
<n-p>
|
||||
For more information about <code>sed</code> command, see <router-link target="_blank" to="/sed-memo">
|
||||
Sed Command Cheatsheet
|
||||
</router-link>
|
||||
</n-p>
|
||||
</n-space>
|
||||
|
||||
<n-form :model="form" label-placement="left" label-width="120px" mt-2>
|
||||
<n-space justify="center" mb-1 mt-1>
|
||||
<n-form-item label="Command Type:">
|
||||
<n-radio-group v-model:value="form.commandType" name="commandType">
|
||||
<n-radio-button value="substitute">
|
||||
Substitute
|
||||
</n-radio-button>
|
||||
<n-radio-button value="delete">
|
||||
Delete
|
||||
</n-radio-button>
|
||||
<n-radio-button value="extract">
|
||||
Extract
|
||||
</n-radio-button>
|
||||
<n-radio-button value="number">
|
||||
Number Lines
|
||||
</n-radio-button>
|
||||
</n-radio-group>
|
||||
</n-form-item>
|
||||
</n-space>
|
||||
|
||||
<n-card v-if="form.commandType !== 'number'" title="Search Pattern:">
|
||||
<n-input v-model:value="form.pattern" placeholder="e.g., foo" />
|
||||
<router-link target="_blank" to="/regex-memo">
|
||||
See Regex Cheatsheet
|
||||
</router-link>
|
||||
</n-card>
|
||||
|
||||
<n-form-item v-if="form.commandType === 'substitute'" label="Replacement:" mt-3>
|
||||
<n-input v-model:value="form.replacement" placeholder="e.g., bar" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item v-if="form.commandType === 'substitute'" label="Delimiter:">
|
||||
<n-input v-model:value="form.delimiter" placeholder="e.g., / or #" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item v-if="form.commandType === 'substitute'" label="Flags:">
|
||||
<n-checkbox v-model:checked="form.global">
|
||||
Global (g)
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="form.caseInsensitive">
|
||||
Case Insensitive (i)
|
||||
</n-checkbox>
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Options:">
|
||||
<n-checkbox v-model:checked="form.inPlace">
|
||||
In-place Editing (-i)
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="form.extendedRegex">
|
||||
Extended Regex (-E)
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="form.suppressPrint">
|
||||
Suppress Automatic Printing (-n)
|
||||
</n-checkbox>
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Target File">
|
||||
<n-input v-model:value="form.targetFile" placeholder="e.g., file.txt" />
|
||||
</n-form-item>
|
||||
|
||||
<c-card title="Generated Sed command:" mt-3>
|
||||
<textarea-copyable :value="sedCommand" />
|
||||
</c-card>
|
||||
</n-form>
|
||||
</div>
|
||||
</template>
|
||||
@@ -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',
|
||||
});
|
||||
@@ -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)
|
||||
@@ -0,0 +1,25 @@
|
||||
<script setup lang="ts">
|
||||
import { useThemeVars } from 'naive-ui';
|
||||
import Memo from './sed-memo.md';
|
||||
|
||||
const themeVars = useThemeVars();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<Memo style="overflow-x: auto;" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
::v-deep(pre) {
|
||||
margin: 0;
|
||||
padding: 15px 22px;
|
||||
background-color: v-bind('themeVars.cardColor');
|
||||
border-radius: 4px;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
::v-deep(a) {
|
||||
color: v-bind('themeVars.textColor3');
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user