How to use variables in 'sed' append command? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to use variables in 'sed' append command?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-15-2013
mjavalkar mjavalkar is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 9 July 2013, 5:08 PM EDT
Posts: 25
Thanks: 11
Thanked 0 Times in 0 Posts
How to use variables in 'sed' append command?

HELLO!!

I'm trying to pass a variable with in the 'sed' command (which would add some piece of code to file at a particular line). We can use


Code:
sed '{line-number}a\
alfjaljf\
aslfjsfsjafl\
adlfjaf\' file.txt

If file.txt is
Quote:
<Property>
<name>abc</name>
<value>10</value>
</Property>
<Property>
<name>xyz</name>
<value>23</value>
</Property>
<Property>
<name>uvw</name>
<value>08</value>
</Property>
Now, I would like to add the parameter 'lmn' after 'abc' property. I tried this:

Code:
sed '4a\
\t<Property>\
\t\t<name>lmn</name>\
\t\t<value>90<\/value>\
\t<\/Property>' file.txt > file1.txt

But I want to grep the line number of 'abc' and then based on the line number i would like to perform the append using sed command.
here is my code:

Code:
#! /bin/bash
a=`grep -n "<name>abc</name>" file.txt` | awk '{print $1}'`
b=`echo ${a%?}`
c=`expr b+2`
sed '$ca\
\t<Property>\
\t\t<name>lmn</name>\
\t\t<value>90<\/value>\
\t<\/Property>' file.txt > file1.txt

But this is not working. Can anyone help me please???
Sponsored Links
    #2  
Old 01-15-2013
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 23 July 2014, 12:31 AM EDT
Posts: 2,728
Thanks: 101
Thanked 844 Times in 795 Posts
Put $c outside of single quotes:


Code:
#! /bin/bash
a=`grep -n "<name>abc</name>" file.txt` | awk '{print $1}'`
b=`echo ${a%?}`
c=`expr b+2`
sed $c'a\
\t<Property>\
\t\t<name>lmn</name>\
\t\t<value>90<\/value>\
\t<\/Property>' file.txt > file1.txt

The Following User Says Thank You to Chubler_XL For This Useful Post:
mjavalkar (01-15-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to append something to a word using sed command Dpu Shell Programming and Scripting 6 07-26-2011 05:43 AM
Variables in SED command koti_rama Shell Programming and Scripting 2 01-07-2011 09:58 AM
What's wrong with this sed command? delete & append minifish Shell Programming and Scripting 5 04-08-2008 09:34 PM
sed command not work with variables? MaestroRage UNIX for Dummies Questions & Answers 1 02-13-2008 12:55 AM
assigning variables in sed command wxornot Shell Programming and Scripting 3 02-04-2006 01:56 PM



All times are GMT -4. The time now is 07:54 AM.