diff --git a/README.md b/README.md index 65399fb6..fc9d9343 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/r 统计各个`TCP`连接状态的个数。用于方便排查系统连接负荷问题。 1. [xpl and xpf](docs/shell.md#-xpl-and-xpf) 在命令行中快速完成 在文件浏览器中 打开/选中 指定的文件或文件夹的操作,优化命令行与其它应用之间的操作流。 +1. [pb2json](docs/shell.md#-pb2json) `Shell`开发/测试加强: diff --git a/bin/pb2json b/bin/pb2json new file mode 100755 index 00000000..35307644 --- /dev/null +++ b/bin/pb2json @@ -0,0 +1,139 @@ +#!/usr/bin/env bash +# @Function +# Split input content into multiple JSON files with specified item counts. +# Supports both clipboard input and direct string input with newlines. +# +# @Usage +# $ ./pb2json [input_content] +# $ ./pb2json 3 5 "1111\n1111\n1111" +# $ ./pb2json 2 3 "$(pbpaste)" +# +# @author xiongzhongwei +set -eEuo pipefail + +readonly PROG=${0##*/} +readonly PROG_VERSION='1.1.1' + +usage() { + cat < [input_content] + +Split input content into multiple JSON files with specified item counts. +Files will be saved in the current directory. + +Arguments: + file_count Number of output files to create + items_per_file Number of items each output file should contain + input_content (Optional) Direct input with newline-separated items. + If not provided, will read from clipboard. + +Example: + $PROG 3 5 "1111\n1111\n1111" # Direct string input + $PROG 2 3 # Read from clipboard + $PROG 2 3 "$(pbpaste)" # Explicit clipboard input + +Options: + -h, --help display this help and exit + -V, --version display version information and exit +EOF + exit +} + +progVersion() { + printf '%s\n' "$PROG $PROG_VERSION" + exit +} + +# Main logic +if [[ "$#" -eq 1 ]]; then + case "$1" in + -h|--help) usage ;; + -V|--version) progVersion ;; + esac +fi + +if [[ "$#" -lt 2 || "$#" -gt 3 ]]; then + echo "Error: Requires 2-3 arguments (file_count, items_per_file, [input_content])" >&2 + usage + exit 1 +fi + +file_count=$1 +items_per_file=$2 +input_content=${3:-} + +if ! [[ "$file_count" =~ ^[0-9]+$ ]] || ! [[ "$items_per_file" =~ ^[0-9]+$ ]]; then + echo "Error: Both arguments must be positive integers" >&2 + exit 1 +fi + +if [[ "$file_count" -eq 0 ]] || [[ "$items_per_file" -eq 0 ]]; then + echo "Error: Arguments must be greater than 0" >&2 + exit 1 +fi + +# Get and process input +if [[ -z "$input_content" ]]; then + if command -v pbpaste &>/dev/null; then + input_content=$(pbpaste) + elif command -v xclip &>/dev/null; then + input_content=$(xclip -o -selection clipboard) + elif command -v powershell.exe &>/dev/null; then + input_content=$(powershell.exe -command "Get-Clipboard") + else + echo "Error: No clipboard tool found and no direct input provided" >&2 + exit 1 + fi +fi + +if [[ -z "$input_content" ]]; then + echo "Error: Input is empty" >&2 + exit 1 +fi + +# Convert all types of newlines to uniform format +processed_content=$(printf "%b" "$input_content") + +# Read into array +items=() +while IFS= read -r line; do + [[ -n "$line" ]] && items+=("$line") +done <<< "$processed_content" + +total_items=${#items[@]} +if [[ "$total_items" -eq 0 ]]; then + echo "Error: No valid items found in input" >&2 + exit 1 +fi + +# Calculate file count +required_file_count=$(( (total_items + items_per_file - 1) / items_per_file )) +if [[ "$file_count" -gt "$required_file_count" ]]; then + echo "Warning: Reducing file count from $file_count to $required_file_count" + file_count=$required_file_count +fi + +# Generate files +current_dir=$(pwd) +for ((i=0; i/dev/null; then + printf '%s\n' "${items[@]:start:items_per_file}" | jq -R . | jq -s . > "$json_file" + else + # Fallback without jq + printf '[' > "$json_file" + for ((j=start; j> "$json_file" + printf '"%s"' "$(sed 's/"/\\"/g' <<< "${items[j]}")" >> "$json_file" + done + printf ']\n' >> "$json_file" + fi + + actual_items=$(( (j-start) )) + echo "Created: $json_file ($actual_items items)" +done + +echo "Success: Split $total_items items into $file_count JSON files in $current_dir" \ No newline at end of file diff --git a/docs/shell.md b/docs/shell.md index 6d8b0e39..d552be91 100644 --- a/docs/shell.md +++ b/docs/shell.md @@ -474,6 +474,44 @@ xpf /path/to/dir1 /path/to/foo1.txt - [Linhua Tan](https://github.com/toolchainX) 修复Linux的选定Bug。 + +🍺 [pb2json](../bin/pb2json) and [pb2json](../bin/pb2json) +---------------------- +`pb2json`用于将剪贴板或直接输入的文本内容按指定数量分割为多个JSON格式文件,每个文件包含指定数量的数组元素。 +支持`Linux`、`Mac`、`Windows`。 + +### 用法/示例 +pb2json <文件数量> <每文件条目数> [输入内容] +参数说明 +参数 必选 说明 +文件数量 是 要生成的JSON文件数量 +每文件条目数 是 每个JSON文件包含的条目数 +输入内容 否 直接输入的文本内容(不提供则读取剪贴板) + +```bash +示例1:直接输入文本 +pb2json 2 3 "1111\n2222\n3333\n4444\n5555" +输出: +Created: output_01.json (3 items) +Created: output_02.json (2 items) +Success: Split 5 items into 2 JSON files in /current/directory + + +示例2:读取剪贴板内容 + +# 先复制内容到剪贴板 +printf "apple\nbanana\ncherry\ndate" | pbcopy + +# 执行转换 +pb2json 1 4 + +Created: output_01.json (4 items) +Success: Split 4 items into 1 JSON files in /current/directory +``` +### 贡献者 + +- [xiongzhongwei](https://github.com/nuoyimanaituling) 添加功能支持 + `Shell`开发/测试加强 ====================================