开发者

Replace/append substring with sed

I would like to ask your help!

The problem is:

I have a code: <h1....>text</h1>

I would like to make a result: <h1 ....><a name="Snumber">text</a></h1> where S is character, number is a variable integer.

开发者_StackOverflow社区My problem that I don't know, how to subs non-fix at the beginning of the line, how could I use regular expression for it? Thanks in advance!


If I understood your requirement clearly something like that will work for you in bash:

str='<h1 class="a">text</h1>' # this is your original string
ch='S' # this is your character
n='56789' # this is your number
echo "${str}<${ch}${n}>" | \
  sed 's#\(<h1[^>]*>\)\([^<]*\)\(</h1>\)<\([^>]*\)>#\1<a name="\4">\2</a>\3#'

OUTPUT:
<h1 class="a"><a name="G56789">text</a></h1>

Sorry if there is any misunderstanding on my part then I will happily delete my answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜