Sponsored Content
Top Forums Shell Programming and Scripting How to use variables in 'sed' append command? Post 302756381 by mjavalkar on Tuesday 15th of January 2013 07:23:42 PM
Old 01-15-2013
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??? SmilieSmilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

assigning variables in sed command

I need to assign a variable within a variable in a sed command. I tried doing the following in c shell. set left = 1 set right = 2 set segment = qwerty sed -n -e "/$segment{$left}/,/$segment{$right}/p" file.txt what is wrong with this syntax? (3 Replies)
Discussion started by: wxornot
3 Replies

2. UNIX for Dummies Questions & Answers

sed command not work with variables?

I am trying to write a simple script which will take a variable with sed to take a line out of a text and display it #!/bin/sh exec 3<list while read list<&3 do echo $list sed -n '$list p'<list2 done this does not work, yet when I replace the $list variable from the sed command and... (1 Reply)
Discussion started by: MaestroRage
1 Replies

3. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies

4. Shell Programming and Scripting

Can i use Variables in sed command in line numbers

I wish to give line number from one point to another in sed command like this sed -n 1,1000000p file1.txt >file2.txt but variable line number $x,$x+100000 can i give it cos i tried and it was giving an error any suggestions?/ Thx in advance AC (2 Replies)
Discussion started by: bezudar
2 Replies

5. Shell Programming and Scripting

sed: command garbled - when executed with $variables

I have got a file job_tree.lst , which contains 1 line as below $ cat job_tree.lst ABC_D_EQ_XXXXX_YYYY_PRD_UX 05/08/2009 05:47 05/08/2009 05:50 IN 13412850/0 I want to get a child job string 3 characters offset from the parent job string (i.e. ABC_D_EQ_XXXXX_YYYY_PRD_UX 05/08/2009... (4 Replies)
Discussion started by: Armaan_S
4 Replies

6. Shell Programming and Scripting

Variables in SED command

Hi all, I want write a script to display 5rows at times from a input file. my out like: echo " display started" r1 r2 r3 r4 r5 ... Some action items... again i need next 5 lines. can you please advise. (2 Replies)
Discussion started by: koti_rama
2 Replies

7. Shell Programming and Scripting

How to append something to a word using sed command

Hi, How to append something to already existing word. Suppose, I have the following line as a part of a file. VVV= jdbc:... (6 Replies)
Discussion started by: Dpu
6 Replies

8. Shell Programming and Scripting

How to append a value to the output after using sed command?

Hi All, I have a file where I am converting newlines to comma separated values but I would like to append zero if the output is empty Here is the command I am using sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' test1.txt test1.txt will have comma seperated values but sometimes this file can be... (6 Replies)
Discussion started by: rajeevm
6 Replies

9. Shell Programming and Scripting

sed command to append word at end of line

hello Team, I am looking for sed command or script which will append word at end of line. for example. I want to validate particular filesystem with mount |<filesystem name> command. if nodev parameter is not there then it should add in the fstab file with receptive to the filesystem. # mount... (8 Replies)
Discussion started by: ghpradeep
8 Replies

10. UNIX for Beginners Questions & Answers

Help using combining variables with sed command (RHEL 7)

Here is the whole script, very simple, but I am just learning ROK_NO=$1 RPT=/tmp/test sed -E '/^SELECT/ s/(.{23}).{8}/\1'"$ROK_NO"' /' $RPT echo $RPT When I run this I get $ bash rok.sh 2388085 : No such file or directory /tmp/test When I type the command in console, it works... (3 Replies)
Discussion started by: isey78
3 Replies
TABLIX(1)						       Tablix User's Manual							 TABLIX(1)

NAME
tablix2_plot - Tablix data plotter SYNOPSIS
tablix2_plot command [ options ] DESCRIPTION
Tablix is a powerful free software kernel for solving general timetabling problems. It uses a coarse-grained parallel genetic algorithm in combi- nation with other techniques to construct sensible timetables from XML formatted problem descriptions. Tablix can run on a single host as well as on a heterogeneous parallel virtual machine using PVM3. tablix2_plot can be used to plot various types of population convergence graphs using data in files usually named conv0.txt , conv1.txt , ... Tablix will save convergence data into these files if it was compiled with the --enable-conv option. These graphs can be useful for example to get a rough estimate of time required to solve a particular problem (note that tablix2_plot can be safely used with files that are still written to by a running Tablix session). Graphs are plotted with gnuplot using the default output terminal (which is in most cases a X11 display). Encapsulated postscript files can be obtained by setting EPSOUTPUT environment variable. Scalable vector graphics files can be obtained by setting SVGOUTPUT environment variable. The contents of these variables are used by tablix2_plot as a file name to write to. OPTIONS
tablix2_plot --conv-fitness [ --scale SCALE ] [ PREFIX ] ... Plot population convergence graph using convergence data in files conv*.txt in the current directory. One line is drawn for each computing node. It shows the fitness value of the best timetable versus generation count. Use the PREFIX option, if you used the -o option with Tablix. You can use more than one PREFIX option to plot multiple convergence graphs in one window. Use the SCALE option to adjust the vertical scale. tablix2_plot --fit-fitness [ --scale SCALE ] [ PREFIX ] ... Same as --conv-fitness above, except that an exponential function is also drawn on the graph using least squares fitting. This can be used to get a rough estimate of the time required to find a solution. The SCALE parameter is used to adjust the horizontal scale in this case. tablix2_plot --functions [ --scale SCALE ] CONVFILE Plot the return value of each fitness function versus generation count. Return values of mandatory fitness functions are plotted with thicker lines. CONVFILE must be a file with Tablix population convergence data (usually named conv*.txt ). Use the SCALE option to adjust the vertical scale. EXAMPLES
If Tablix was started with the following command: tablix2 -o u1_ sample.xml then the following command plots the population convergence graph: tablix2_plot --conv-fitness u1_ Encapsulated postscript version of the same graph can be obtained with the following command in bash shell: EPSOUTPUT="graph.eps" tablix2_plot --conv-fitness u1_ To see which mandatory restriction is preventing Tablix from finding the solution, use the per module plot, possibly with a small vertical scale: tablix2_plot --functions --scale 100 u1_conv0.txt BUGS
In addition to gnuplot tablix2_plot utility depends on a number of common UNIX programs (mktemp, grep, sed, awk, ...) that may not be available on all systems. The script is not currently capable of properly detecting if any required programs are missing. AUTHOR
Tomaz Solc (tomaz.solc@tablix.org) SEE ALSO
tablix2(1), Tablix User's Manual, Tablix modules HOWTO, Tablix on Morphix HOWTO Tomaz Solc 2005-09-03 TABLIX(1)
All times are GMT -4. The time now is 03:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy