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

如何在csh中替换set xx {}区间内的指定字符串?

CSH Script to Modify set xx Block Content

Got it, let's tackle this problem. We can use sed (a stream editor perfect for text manipulation) within a csh script to target exactly the lines inside the set xx { \ ... } block and apply the required pattern replacement.

The Script

Save this as modify_set_xx.csh:

#!/bin/csh

# Check if input file is provided
if ($#argv != 1) then
    echo "Usage: $0 <input_file_path>"
    exit 1
endif

# Define input and output files
set input_file = $1
set output_file = "${input_file}.modified"

# Use sed to process the target block
sed '/set xx { \\/,/}/ {
    # Skip the opening and closing lines of the set xx block
    /set xx { \\/! {
        /}/! {
            # Replace pattern: wrap the second-last directory in * and add * at line end
            s/\(.*\/\)\([^\/]*\)\(\/[^\/]*\) \\/\1*\2*\3* \\/
        }
    }
}' $input_file > $output_file

echo "Processing done! Modified content saved to: $output_file"

How It Works

  • Range Selection: The sed command first targets all lines between /set xx { \\/ (the start of your block) and /}/ (the end of the block).
  • Skip Boundary Lines: We exclude the opening set xx { \ line and closing } line from modification.
  • Pattern Replacement: For each middle line:
    • \(.*\/\): Captures everything up to the second-last / (e.g., a/ from a/c/d, apple/d/ from apple/d/e/g)
    • \([^\/]*\): Captures the second-last directory name (e.g., c, e)
    • \(\/[^\/]*\): Captures the last / and directory name (e.g., /d, /g)
    • We rewrite the line to wrap the second-last directory in * and append a * at the end before the trailing \.

How to Use

  1. Make the script executable:
    chmod +x modify_set_xx.csh
    
  2. Run it with your input file:
    ./modify_set_xx.csh your_input_file.txt
    
  3. The modified content will be saved to your_input_file.txt.modified.

Testing with Your Sample Input

If you run this script on your sample input, it will produce exactly the output you specified—only modifying the lines inside the set xx block while leaving other set commands untouched.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:18:27