Find the answer to your Linux question:
Results 1 to 4 of 4
I have a file which looks like this: Code: <components> <folder name="bin"/> <components> and I would like to add one line as shown below: Code: <components> <folder name="bin"/> <components> <component ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59

    sed append/insert

    I have a file which looks like this:
    Code:
    <components>
    <folder name="bin"/>
    <components>
    and I would like to add one line as shown below:
    Code:
    <components>
    <folder name="bin"/>
    <components>
    <component name="Server" />
    How can I do it using sed?

  2. #2
    Just Joined!
    Join Date
    Oct 2008
    Posts
    21
    you can do that with echo "line you want to add " >> filetoadd
    but with sed you can add this way
    The "a" command appends a line after the range or pattern. This example will add a line after every line with "WORD:"

    #!/bin/sh
    sed '
    /WORD/ a\
    Add this line after every line with WORD
    '

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    First, I don't have just 3 lines in my files, its 100 lines file and I just showed few lines here. So the first method shouldn't work.
    The second one will append the line with every line with WORD. I don't want that also. I just want to add a line after a line containing <components>, but that line containing component should be after "<folder name=bin>"
    Please see the 2 files again

  4. #4
    Just Joined!
    Join Date
    Oct 2008
    Posts
    21
    you didn't explained that before, but here is your solution

    sed '/<folder name=bin>/ {
    N
    /<components>/ {
    a\
    Add this line after
    }
    }' file

Posting Permissions

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