Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to use variables in 'sed' append command?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-15-2013
mjavalkar mjavalkar is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 14 April 2015, 7:53 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??? LinuxLinux
Sponsored Links
    #2  
Old Unix and Linux 01-15-2013
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 1 September 2015, 6:35 PM EDT
Posts: 3,220
Thanks: 126
Thanked 1,072 Times in 1,005 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:33 AM.