Find the answer to your Linux question:
Results 1 to 5 of 5
I have a file called file1 file: Code: component In file1, I want to append one line after the first line which contains 'component' and store it in a file2. ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59

    sed and variable value with a space issue

    I have a file called file1
    file:
    Code:
    component
    In file1, I want to append one line after the first line which contains 'component' and store it in a file2. So my file 2 should like this:
    file2:
    Code:
    components
    <component name="DB Tools" displayDescription="false ">
    This is my script which should do the above things.
    script:
    Code:
    var="DB Tools"
    sed '/components/a\
    <component name="'$var'" displayDescription="false ">
    ' < file1 > file2
    When I run the script I get following error:
    sed: input file "Tools" displayDescription="false ">
    ": The system cannot find the file specified.

    How can we fix the above error without using IFS?

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Try this:

    Code:
    sed '/components/a\
    <component name="'"$var"'" displayDescription="false ">
    ' < file1 > file2

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Thanks, it worked! Sed sometimes looks tricky.
    I was planning to use ant for the same purpose, but didn't find task which can so this. But that is another topic and need not be discussed here.

  4. #4
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by sandeep t View Post
    Thanks, it worked! Sed sometimes looks tricky.
    I'm not going to contradict that, but the problem here is not related to sed at all. It is related to quoting.

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Agreed.

    I was wondering how can we use sed append when the string after which I want to add a line is present in two lines.
    For ex, this is my script:
    Code:
    <components>
    <folder name="bin"/>
    <components>
    I want append a line and the output file should look like this:
    Code:
    <components>
    <folder name="bin"/>
    <components>
    <component name="Server" />
    Ofcourse, blindly I can't use
    sed '/<components>/a\
    <component name="Server"'

    Can I take the reference of "folder name=bin" line?

Posting Permissions

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