XML Copy & replace problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML Copy & replace problem
# 1  
Old 06-14-2008
XML Copy & replace problem

I probably could have done this at one time, but, the years and no need has left my scripting skills lacking and I'm unable to work this problem out. https://www.unix.com/images/smilies/frown.gif
Smilie

Using Linux, have a great many xml files in which there may be multiple occurrence of a line of the form:

<sub_unit type="County" name="Campbell" geog_id="null">

The xml filename that contains these lines is of the form:

ky.abc.123.xml

Where the first two letters are state abbreviations.

What I need to do is grab the first two letters of the filename,
Add a code letter, for County it would be "C" and
Combine them, with an underscore character, to the contents of the name field and
Insert it into the geog_id field, replacing the word null,
so the new line looks like:

<sub_unit type="County" name="Campbell" geog_id="KYC_Campbell">

Any and all help greatly appreciated.
# 2  
Old 06-15-2008
Try this:

Code:
awk -F'"' '
   NR==1 {
      OFS=FS;
      split(FILENAME, x, ".");
      prefix=toupper(x[1]) "C_";
   }
   /<sub_unit / {
      $6=prefix $4;
   }
   {
      print;
   }
' ky.abc.123.xml

# 3  
Old 06-15-2008
Thanks Robotronic,
will give it a try on Monday at work.
I only have XP at home. Smilie
Will let you know how it turns out.


Thanks again.
# 4  
Old 06-15-2008
Robotronic,

If I could bother you a bit more?
I pretty much understand what is going on, but there are a few areas that are still a bit murky.
If I am reading it right, the script breaks down as:

The -F '"' sets the field separator to a quote mark

The entire script is enclosed in a single quote or is it a back tick??

The NR==1 does ??

The OFS=FS; sets the Output Field separator equal to the Field separator or a quote mark.

The split(FILENAME, x, ".") sets the variable x with the first two character of the filename, using the . as field separator.

The prefix=toupper(x[1]) "c_"; sets the variable prefix to the contents of the variable x which has been converted to upper case and adds the "C_".

The /<sub_unit / { finds the lines to be modified???

$6=prefix $4; sets field 6 to the contents of the variable prefix and the contents of field number 4.

print sends it to output. Does it overwrite the existing xml or should it write to a temporary file and then move the temp to overwrite the original??

Again, thanks a million, I really appreciate the assistance. Smilie
# 5  
Old 06-16-2008
Quote:
The entire script is enclosed in a single quote or is it a back tick??
awk scripts are always enclosed in single quotes.

Quote:
The NR==1 does ??
The code in curly braces following this expression is executed only once, for the first loop (= for the first record of the input file = NR).

Quote:
The split(FILENAME, x, ".") sets the variable x with the first two character of the filename, using the . as field separator.
Quite right... "x" is an array which elements are all the dot-separated parts of the file name. So, x[1]="ky", x[2]="abc", and so on.

Quote:
The /<sub_unit / { finds the lines to be modified???
For every line that matches the pattern enclosed in slashes, executes the code which modifies the 6th field.

Quote:
print sends it to output. Does it overwrite the existing xml or should it write to a temporary file and then move the temp to overwrite the original??
The output is sent to stdout, so your original file will not be overwrited: you need a temporary file as you said.

Smilie
# 6  
Old 06-17-2008
What am I doing wrong here?
First off, I'm using Cygwin on my XP to run the script, so that could be part of the problem.

I modified the script a bit and this works, from the command line,
but no matter how I tried, I could not get it to run from a script.

=================================
for i in ls *.xml; do
cat `$i` | awk -F'"' '
NR==1 {
OFS=FS;
split(FILENAME, x, ".");
prefix=toupper(x[1]"C_");
}
/[Cc][Oo][Uu][Nn][Tt][Yy]/ {
$6=prefix $4;
}
{
print
}'
done
=================================

The first problem, is it is not reading the FILENAME.
The x variable contains only a dash character and "C_".

The second problem is I can't re-direct the output.
I tried using:

/[Cc][Oo][Uu][Nn][Tt][Yy]/ {
$6=prefix $4;
}
{
print
}' > temp
done

but it won't work, it only creates an empty file, yet I can pipe it thru less and it displays to the screen.

/[Cc][Oo][Uu][Nn][Tt][Yy]/ {
$6=prefix $4;
}
{
print
}' | less
done

How can I get it to write to a file?

Thanks for any pointers.
All help appreciated.
# 7  
Old 06-18-2008
Unfortunately I don't have Cygwin installed on my Windows system anymore, but I remember that when I was playing around with awk in a shell script I had the same issues, but I've never investigated too much for solving the problem.

Sorry, I can't help you.

Only to inform you that at least you're not alone Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want replace string <enclosure>&#x22;</enclosure> with <enclosure>&#x5e;</enclosure> in xml

I have xml files with with extension .ktr in subfolders i want to replace the string <enclosure>&#x22;</enclosure> with <enclosure>&#x5e;</enclosure> i have written logic but it is not working correctly sed -i '' 's#<enclosure>&\#x22;</enclosure>#<enclosure>&\#x5e;</enclosure>#g' *.ktr ... (3 Replies)
Discussion started by: reddy12
3 Replies

2. Shell Programming and Scripting

Compare File & Copy Replace if Successful

Hi All, I have written a shell script that creates a backup of my MySQL database. The script performs the following functions: Creates a Backup of the MySQL database Compresses the Backup Copies the Backup to a Remote Server Send an E-Mail displaying the size of the Backup Removes any... (6 Replies)
Discussion started by: SalientAnimal
6 Replies

3. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

4. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

5. Red Hat

copy & replace text

how can i copy a certain word from a text file then use this word to replace in another text file?? i tried to use something like: awk '{print "Hit the",$1,"with your",$2}' /aaa/qqqq.txt > uu.txt but i can't add an argument to point to the second file which i will replace in. please... (8 Replies)
Discussion started by: mos33
8 Replies

6. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

7. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

8. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

9. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

10. UNIX for Dummies Questions & Answers

vi search & replace ... having '/' in string - problem.

I want to carry out search & replace for the paths mentioned in the file with the help of vi. 'abc/' to be replaced by 'abc/data' When I use command in vi as below - %s/abc//abc/data/g it gives me an error. How we should deal with '/' part in string for vi search & replace? ... (6 Replies)
Discussion started by: videsh77
6 Replies
Login or Register to Ask a Question