0

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.

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!

Peter
  • 1
  • 1
    I can forward you to this http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags which is quite interesting topic and will give you a good overview. – topless Apr 09 '11 at 15:22
  • What are S and number going to be determined by? Something internal to the string or are you calling this in a loop or what? – drysdam Apr 09 '11 at 15:22
  • What is S and number? A result must be in S1, S2, S3 etc. but it is not a problem.:-) – Peter Apr 09 '11 at 15:52
  • If I try another example: I would like to take this: every word with beginning asa change asa*newchars, where * is a second part of original string. – Peter Apr 09 '11 at 15:52
  • If you think awk is better for this, it is no problem! – Peter Apr 09 '11 at 16:02

1 Answers1

0

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hi! Thanks for your help. The problem is that I read it from file, where your str is variable:-( The simpler is

    , there is no problem. But sometimes is

    or any other strings after

    So I need to replace

    to

    Is it clear?

    – Peter Apr 09 '11 at 18:41
  • @Peter: If you see my answer it takes care of all `` not just `

    `. Even my example has `

    text

    `. Of course this string can be read from file as well in bash.
    – anubhava Apr 09 '11 at 21:11