我正在尝试从某个SED组中删除某个角色。
例如
sed 's/\(group1\)/\(group2\)/ \1 \2/'
我希望在显示第2组时删除某个字符。
谢谢!
答案 0 :(得分:0)
好。所以我不得不在这里做一些假设。即...
我最初把它当作学术练习。这就是我想出来的......
#I define the below values in variables for the sake of making this code
#more easily altered to suit your use case. You could of course make this
#a true "one-liner" by hard coding the final command with the values.
#also, the initial echo could really be any command producing that output
#being piped into what is in the curly braces
group_delim="-"
rm_char="_"
echo "F_I_RST-HEL_L_O" | { input=$(< /dev/stdin) && first=$(echo ${input} | sed -n "s/\(.*\)\(${group_delim}.*\)/\1/p") && printf ${first}${group_delim} && second=$(echo ${input} | sed -n "s/\(.*${group_delim}\)\(.*\)/\2/p") && echo $second | sed "s/${rm_char}//g" ; }
输出结果为:
F_I_RST-HELLO
当然这是整个字符串,只有第二个组(由分隔符定义),并删除了所有定义的“rm_char”。