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. ...
- 10-17-2008 #1Just Joined!
- Join Date
- Apr 2007
- Posts
- 59
sed and variable value with a space issue
I have a file called file1
file:
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:Code:component
file2:
This is my script which should do the above things.Code:components <component name="DB Tools" displayDescription="false ">
script:
When I run the script I get following error:Code:var="DB Tools" sed '/components/a\ <component name="'$var'" displayDescription="false "> ' < file1 > file2
sed: input file "Tools" displayDescription="false ">
": The system cannot find the file specified.
How can we fix the above error without using IFS?
- 10-17-2008 #2Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Try this:
Code:sed '/components/a\ <component name="'"$var"'" displayDescription="false "> ' < file1 > file2
- 10-17-2008 #3Just 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.
- 10-17-2008 #4Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
- 10-21-2008 #5Just 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:
I want append a line and the output file should look like this:Code:<components> <folder name="bin"/> <components>
Ofcourse, blindly I can't useCode:<components> <folder name="bin"/> <components> <component name="Server" />
sed '/<components>/a\
<component name="Server"'
Can I take the reference of "folder name=bin" line?


Reply With Quote
