You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Bash脚本结合sed按URL删除JSON对象块的技术问询

Solution: Delete JSON Object by URL in Bash Script

Got it, let's break down how to build your cert-check-script-delete Bash script to remove a specific JSON object by URL. I'll cover two approaches: one using sed (as you requested) and a more robust alternative with jq (since JSON is structured data, text-based tools like sed can have edge cases).

1. Using sed (as requested)

This script will take a URL as an argument, find the corresponding JSON object block in your file, and delete it. We'll handle special characters in URLs and create a backup of your original file.

Final Script

#!/bin/bash

# Check if user provided a URL argument
if [ $# -ne 1 ]; then
    echo "Usage: $0 <url-to-delete>"
    exit 1
fi

TARGET_URL="$1"
JSON_FILE="your-cert-data.json"  # Replace with your actual JSON file path

# Escape special characters (like /, ", &) in the URL for safe sed matching
ESCAPED_URL=$(printf '%s' "$TARGET_URL" | sed 's/[\/&"]/\\&/g')

# Delete the matching JSON object block
# Note: For macOS, replace `-i.bak` with `-i '' -e` (see notes below)
sed -i.bak "/{/,/}/ { /\"url\" : \"$ESCAPED_URL\"/ { :a; N; /}/! ba; d; } }" "$JSON_FILE"

echo "✅ Successfully deleted entries for URL: $TARGET_URL"
echo "📁 Backup of original file saved to: ${JSON_FILE}.bak"

How It Works

  • Argument Check: First, we verify the user passed exactly one URL parameter to avoid errors.
  • URL Escaping: We escape characters that would break sed (like / or ") so the script works with URLs containing special characters.
  • Sed Command Breakdown:
    • /\{/,/\}/: Targets every block starting with { and ending with } (each individual JSON object in your file).
    • /\"url\" : \"$ESCAPED_URL\"/: Within each block, checks if the URL matches your target.
    • :a; N; /}/! ba: Loops to read the entire block until it hits the closing } (ensures we delete the full object, not just a single line).
    • d: Deletes the matched block from the file.

macOS Note

If you're running this on macOS, the sed syntax is slightly different. Replace the sed line with this:

sed -i '' -e "/{/,/}/ { /\"url\" : \"$ESCAPED_URL\"/ { :a; N; /}/! ba; d; } }" "$JSON_FILE"

sed is a text-processing tool, not a JSON parser—if your JSON ever changes format (e.g., extra newlines, nested objects), sed might fail. jq is a dedicated JSON tool that handles structured data reliably.

Script with jq

#!/bin/bash

if [ $# -ne 1 ]; then
    echo "Usage: $0 <url-to-delete>"
    exit 1
fi

TARGET_URL="$1"
JSON_FILE="your-cert-data.json"

# Use jq to filter out the target URL and overwrite the file
jq --arg target_url "$TARGET_URL" 'map(select(.url != $target_url))' "$JSON_FILE" > temp.json && mv temp.json "$JSON_FILE"

echo "✅ Successfully deleted entries for URL: $TARGET_URL"

Why This Is Better

  • jq parses the JSON structure directly, so it doesn't care about whitespace or line breaks.
  • It safely handles nested objects or unexpected formatting that would trip up sed.
  • The syntax is more readable for JSON-specific operations.

内容的提问来源于stack exchange,提问作者tnoel999888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:03:24