Results 1 to 5 of 5

Thread: Trying to add some lines to a file.

  1. #1
    Join Date
    Jul 2008
    Beans
    33
    Distro
    Ubuntu

    Trying to add some lines to a file.

    I know I need to find this line:
    Code:
    <a href="tv/recorded"><?php echo t('Recorded Programs') ?></a>
    Then add these two lines underneath that line, each on it's own line

    Code:
    &nbsp; | &nbsp;
    <a href="episode"><?php echo t('TV Episodes') ?></a>

    I know SED would be the way to do so, but I have yet to find an example that I understand. Could someone please provide me with the proper sed statement to add to a bash script to automate this. I have to re-add these after each software update and this would make it easier.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Trying to add some lines to a file.

    Handling HTML/PHP with sed is always a PITA - but it would be hard to come up with a more pathological example than that (double quotes AND single quotes in the pattern, as well as the usual special characters like / and ?). You will need to escape or bracket (turn into literal character ranges) those - which you do where is partly a matter of style. I *think* that these work:

    1) using the sed append (a) command

    Code:
    $ sed "/<a href=\"tv\/recorded\"><[?]php echo t('Recorded Programs') [?]><\/a>/a\your first new line\nyour second new line" yourfile
    2) with the more common sed substitute (s) command (can get rid of the escapes of the slashes by using a different character such as | as the separator)

    Code:
    $ sed "s|<a href=\"tv/recorded\"><[?]php echo t('Recorded Programs') [?]></a>|&\nyour first new line\nyour second new line|" yourfile
    Last edited by steeldriver; October 31st, 2013 at 09:23 AM.

  3. #3
    Join Date
    Jul 2008
    Beans
    33
    Distro
    Ubuntu

    Re: Trying to add some lines to a file.

    I was able to get the second one to "work", but it just spits the content out to the console. How would I have it write it back to the file?

    Code:
    sed "s|<a href=\"tv/recorded\"><[?]php echo t('Recorded Programs') [?]></a>|&\n\&nbsp\; \| \&nbsp\;\n<a href=\"episode\"><[?]php echo t('TV Episodes') [?]></a>|" header.php

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Trying to add some lines to a file.

    add -i switch for in-place modification
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jul 2008
    Beans
    33
    Distro
    Ubuntu

    Re: Trying to add some lines to a file.

    Quote Originally Posted by Vaphell View Post
    add -i switch for in-place modification
    Thanks. I swear I looked at the online documentation for sed and didn't see that. I guess new stuff is often overwhelming at first.

Bookmarks

Posting Permissions

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