Find the answer to your Linux question:
Results 1 to 5 of 5
I'd like to automate formatting some text. So I hope someone can help me out on this as googling didn't help me much. I'd like to find out how to ...
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29

    Wrap tag around regex

    I'd like to automate formatting some text.
    So I hope someone can help me out on this as googling didn't help me much.
    I'd like to find out how to wrap a tag around regex.
    For example if I have word "Title" in the text to get something like <div id="title>Title</div>. I would know how to handle it if it was just "Title".
    But in most cases I have "Title 2" or "Title 2 & 3" so I would need something to insert
    <div id="title"> above the line containing the regex and to insert </div> under that line to cover complete title.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The context of your situation is not clear, it is best to include a sample of the input and desired output.

    Regards

  3. #3
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Ok, I'll try to clarify. To simplify let's say I have 3 songs in a text document. And they look something like this:

    Code:
    Song 1
    Some lyrics
    more lyrics
    and some more
    Code:
    Song 2
    Some lyrics
    more lyrics
    and some more
    Code:
    Third Song
    Some lyrics
    more lyrics
    and some more
    I'd like to make a script which would allow me insert opening div tag before a line that contains "Song" and closing tag after that line. Regardless of other characters which that line might contain, like number 1 or word "Third".
    To get something like this:
    Code:
    <div id="title">
    Song 1
    </div>
    Some lyrics
    more lyrics
    and some more
    I hope that makes it more clear.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    I'm not on a linux box so I can't test it out, but try this:

    Code:
    awk '/Song/{print "<div id=\"title\">";print;print "</div>";getline} 1' file
    Regards

  5. #5
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Thanks a lot Franklin52, that does the job.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...