首页 > 解决方案 > 如何在csh中的2个字符串之间更改字符串?

问题描述

我有如下输入文件,我希望在set xx { \&之间替换字符串,}如输出所示。如何在 csh 中编码?

输入:

set a 1
set b 2
set xx { \
a/c/d \
apple/d/e/g \
guava/s/s/g/b/c \
}
set c 3

输出:

set a 1
set b 2
set xx { \
a/*c*/d* \
apple/d/*e*/g* \
guava/s/s/g/*b*/c* \
}
set c 3

标签: csh

解决方案


$ cat fixer.sed
#!/bin/sed -f
        /set xx {/ , /}/ {
           s@\(/.*\)\([^/]\)\(/[^/]*\)\(  *[\]\)$@\1*\2*\3*\4@
        }

/set xx {/ , /}/是一个范围表达式,sed扫描每一行,但只s@...@...@对提供的范围之间的行执行操作。

$ chmod +x fixer.sed  # only the first time you create file

输出

$ ./fixer.sed txt.txt
set a 1
set b 2
set xx { \
a/*c*/d* \
apple/d/*e*/g* \
guava/s/s/g/*b*/c* \
}
set c 3

推荐阅读