Replacing text between Tags, with output from a complex command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text between Tags, with output from a complex command
# 1  
Old 08-17-2010
Replacing text between Tags, with output from a complex command

The Problem

I have n files all named "conf.cfg", each of which contain a line that reads: "MyVar=XXX", where XXX can be any value.

I have a second file called report.xml, that might read:
<Reports>
<Report>Report1</Report>
<Report>Report2</Report>
<Report>Report3</Report>
</Reports>

I want to replace everything between the <Reports> tag, with with all the new values I find.

What I have so far:

find . -name conf.cfg | xargs grep -l MyVar= | xargs sed 's/MyVar=*/<Report>/;s/$/<\/Report>/g

This will output for example:

<Report>dog</Report>
<Report>cat</Report>
<Report>mouse</Report>

However, I am unable to take the next logical step and insert that between the <Reports> tag in report.xml.

I tried a whole bunch of things... including

awk '/<Reports>/{p=1;print;print "'"$MyEnvironmentVar"'" }/<\/Reports>/{p=0}!p' report.xml

where $MyVar is the output of the find command... unfortunately, that doesn't work.



I could really use a hand here.

Last edited by Flang; 08-17-2010 at 11:57 AM..
# 2  
Old 08-17-2010
You can do something like that :
Code:
VarName=MyVar

find . -name 'Flang_*.cfg'        |
xargs grep "${VarName}" /dev/null |
awk -v name="${VarName}" '
BEGIN { pattern = "^[^:]*:" name "=" }
NR==FNR {
   sub(pattern, "");
   val[++cnt] = $0;
   next
}
/<Report>/ { next }
/<Reports>/ {
   print;
   for (i=1; i<=cnt; i++) printf "<Report>%s</Report>\n", val[i];
   next;
}
1
' - Flang.xml

Xml input file (Flang.xml) :
Code:
<Doc>
<Reports>
<Report>Report1</Report>
<Report>Report2</Report>
</Reports>
</Doc>

Cfg input files (Flang_*.cfg):
Code:
==> Flang_1.cfg <==
Var=value
MyVar=dog
AnotherVar=empty

==> Flang_2.cfg <==
Init=/home
Color=black
MyVar=cat

==> Flang_3.cfg <==
MyVar=mouse

==> Flang_4.cfg <==
MyVar=horse

Output:
Code:
<Doc>
<Reports>
<Report>dog</Report>
<Report>cat</Report>
<Report>mouse</Report>
<Report>horse</Report>
</Reports>
</Doc>

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Query on using command "sed" for replacing text in bash shell

Re: Query on using command "SED" for replacing text in bash shell While using the command "sed" for find and replace, I wanted to know how one could find a constant and replace it with a variable inside the quotation syntax of sed? I wanted to replace constant 3 with variable name... (3 Replies)
Discussion started by: achandra81
3 Replies

2. Shell Programming and Scripting

Complex Output Filtering

Hi, I have this command on my linux jmap -heap $pid | grep '%\|:' the output of which is like below: I need a smart way to check if any of these memory usage crosses 95%, 90% and 85% i need to triggerAlert accordingly. I know how to trigger email alerts however I need a good way to... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. Shell Programming and Scripting

get specific information from text file or command output

Hello, I would need some help, :wall: on a linux script, I am not sure how can I separate some text file, Text file contains something similar to this: share "userhome_e" "/fs1_100g/FILE58/userhome" umask=022 maxusr=4294967295 netbios=FILE58 share "bu share"... (3 Replies)
Discussion started by: nakaedu
3 Replies

4. Shell Programming and Scripting

Problem while replacing text between two tags

Hi, I have a file i which i want to replace {gIntegraHost} to {gTestRunHost} between <TCPSampler and curl only. I am firing below command sed '1,/<TCPSampler/!{ /curl/,/<TCPSampler/!s/{gIntegraHost}/{gTestRunHost}/; }' file1 But only on instance is getting replaced not all. Any idea how... (6 Replies)
Discussion started by: abhitanshu
6 Replies

5. Shell Programming and Scripting

Replacing text in Perl given by command line

Hi I need to write a Perl script that the file given as first argument of the command line that will find all occurrences of the string given as the third argument of the command line and replace with the string given as the fourth argument. Name newfound file is specified as the second... (3 Replies)
Discussion started by: nekoj
3 Replies

6. Shell Programming and Scripting

Replacing Complex Characters in vi

ok, so i have a number of commands like the below in a script: /usr/bin/awk "/ I need to replace the bolded with "^\ but i'm having tremendous difficulties accomplishing this. This needs to be done in vi. can someone please help me out? (4 Replies)
Discussion started by: SkySmart
4 Replies

7. UNIX for Advanced & Expert Users

Complex Input/Output Redirect

Hi All, Sorry if the title is not good but I did not know how to explain with only some words! What I meant is: I have a unix command built from a private application vendor that when executed it prompts for two entries by the keyboard, let's say, for example: ... (1 Reply)
Discussion started by: felipe.vinturin
1 Replies

8. Shell Programming and Scripting

Complex search and output

Hi, I have somewhat of a complex task at hand. I am on a HP-UX box and I need to search for information embedded in files, further embedded into a file/folder structure. I compiled a list of information that I need found: File=list.csv DATE FIELD 1 FIELD2... (2 Replies)
Discussion started by: doza22
2 Replies

9. Shell Programming and Scripting

In PHP replacing text between TAGS & URI information in Title Tag

Hi, what I am trying to do in PHP, is to replace the Title. I need some of the URL information inside aswell depending on the domain. The title is always different so I need to store it in a variable, put the url info like described below in front of it. Here is an example how it should... (0 Replies)
Discussion started by: lowmaster
0 Replies

10. Shell Programming and Scripting

Add text before lines in command output

Hi2all, I have following command in IBM HMC console: lssyscfg -r prof -m Server-9117-MMA-SN655D350 -F lpar_name,min_mem,desired_mem --header which gives me the following output: lpar_name,min_mem,desired_mem lpar1,1024,2048 lpar2,1024,2048 lpar3,2048,4096 What I want is to add in... (3 Replies)
Discussion started by: UsRb
3 Replies
Login or Register to Ask a Question