Skip to content

+ add pb2json script by xiongzhongwei #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`开发/测试加强:

Expand Down
139 changes: 139 additions & 0 deletions bin/pb2json
Original file line number Diff line number Diff line change
@@ -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 <file_count> <items_per_file> [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 <<EOF
Usage: $PROG <file_count> <items_per_file> [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<file_count; i++)); do
start=$((i * items_per_file))
json_file="output_$(printf "%02d" $((i+1))).json"

# Generate JSON array using jq if available
if command -v jq &>/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<start+items_per_file && j<total_items; j++)); do
[[ $j -gt $start ]] && printf ',' >> "$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"
38 changes: 38 additions & 0 deletions docs/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`开发/测试加强
====================================

Expand Down