Matching and replacing text with case transformations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching and replacing text with case transformations
# 1  
Old 02-18-2010
Matching and replacing text with case transformations

Hi,

I am trying to edit an XML file automatically but my regex and shell script knowledge is very limited. I would appreciate your help fellows.

The XML file has this structure:

HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<map map_file="maps/world.swf" zoom="350%" zoom_x="-115%" zoom_y="-94%" tl_long="-168.49" tl_lat="83.63" br_long="190.3" br_lat="-55.58" >
  <areas>
      <area zoom="1050.7143%" zoom_x="-633.98%" zoom_y="-540.19%" title="AFGHANISTAN" mc_name="AF"></area>
      <area zoom="2992.1429%" zoom_x="-1514.47%" zoom_y="-1098.25%" title="ALAND ISLANDS" mc_name="AX"></area>
 
...

      <area zoom="1071%" zoom_x="-664.6%" zoom_y="-697.61%" title="MALDIVES" mc_name="MV"></area>
      <area zoom="366.9853%" zoom_x="-290.73%" zoom_y="-218.45%" title="NAURU" mc_name="NR"></area>
      <area title="borders" mc_name="borders" color="#FFFFFF" balloon="false"></area>
  </areas>
  <movies>
    <movie file="home" x="50" y="71" url="!/ammap/ammap_data.xml"></movie>
  </movies>  
  <labels>
    <label x="60" y="60" url="!/ammap/ammap_data.xml" remain="true" color_hover="#CC0000" zoom_x="0%" zoom_y="0" zoom="100%">
      <text><![CDATA[<b>Back to world map</b>]]></text>
    </label>
  </labels>
</map>
What I would like to do, is replace each line like:
HTML Code:
<area zoom="1050.7143%" zoom_x="-633.98%" zoom_y="-540.19%" title="NEW ZEALAND" mc_name="AF"></area>
By, the following line, in which :
the country is matched in a token : NEW ZEALAND
partly de-capitalized : New Zealand
a dash is included to replace spaces in multiple-word countries : New-Zealand
and re-used in a an expression as follows:

HTML Code:
<area zoom="1050.7143%" zoom_x="-633.98%" zoom_y="-540.19%" title="NEW ZEALAND" url="http://website.com/w/How-to-Apply-in-New-Zealand?a=w/How-to-Apply-in-New-Zealand" mc_name="NZ"></area>
Thank you for your help with this challenging string manipulation.
Attached is one of the sample files I want make changes in.

Last edited by boonymagique; 02-18-2010 at 08:48 AM.. Reason: wrong attachement -- corrected
# 2  
Old 02-19-2010
You could use something like this:

Code:
perl -i.bck -nle'
BEGIN {
    $mask =
      qw(url="http://website.com/w/How-to-Apply-in-%s?a=w/How-to-Apply-in-%s");
}
if (/(?<=title=")([^"]*)/) {
    @w = $1 =~ /(\w+)/g;
    $n = join "-", map ucfirst "\L$_", @w;
    $url = sprintf $mask, $n, $n;
    s/(title="[^"]*")/$1 $url /;
    print;
}
else {
    print;
}'   ammap_data_europe.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert text between exact matching patterns to Title case

Hi Folks, I have a large text file with multiple similar patterns on each line like: blank">PATTERN1 some word PATTERN2 title=">PATTERN1 some word PATTERN2 blank">PATTERN1 another word PATTERN2 title=">PATTERN1 another word PATTERN2 blank">PATTERN1 one more time PATTERN2 title=">PATTERN1... (10 Replies)
Discussion started by: martinsmith
10 Replies

2. Shell Programming and Scripting

awk Array matching and replacing from master file.

I have an awk related question that I was hoping you all could help with. I am given 2 input files named OLDFILE and NEWFILE, and a Master file named MASTERFILE. They can be seen below. OLDFILE: a a a a a f g 4 5 7 8 1 2 3 (1 Reply)
Discussion started by: tiktak292
1 Replies

3. Shell Programming and Scripting

Matching and Replacing file lines starting with $

Here is the task that I was presented with: I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck. The input deck (deckToChange in the code below) comes with 2 separate files. File 1... (5 Replies)
Discussion started by: tiktak292
5 Replies

4. Homework & Coursework Questions

Matching and Replacing Characters

I need to write an Unix script to report the number of SQL files in my home directory, ending with .sql . The script should, also, be checking whether there is a file with an underscore in its name. If that is the case, the underscore should be converted to a dash (‐); for example... (1 Reply)
Discussion started by: ronoz-4
1 Replies

5. Shell Programming and Scripting

problem in nawk : case insensitive pattern matching

HI, My file contains data something like 034500,5,B5004946544EB185,DEFAULT,0 Now i want to do a pettern match for DEFAULT and remove that particular line from file and transfer the rest contents to temp file.But my req is i want to do case insensitive matching ie DEFAULT / default. I... (4 Replies)
Discussion started by: centurion_13
4 Replies

6. Shell Programming and Scripting

AWK - Pattern Matching & Replacing - Performance

Experts, I am a beginner to Unix Shell Scripting We have source as a flat file which contains CTRL+F character as the delimiter. We need to count the number of records in the file (CTRL+F) to perform file validation Following command being used: awk '{cnt+=gsub(//,"&")}END {print cnt}'... (4 Replies)
Discussion started by: srivijay81
4 Replies

7. Shell Programming and Scripting

Matching and replacing text

Im new to using perl scripting, and i was wondering if anyone could help me, I need to create a cgi file that when it runs it opens a file looks through all the words, and replace the word hello with goodbye, ive been looking around and someone said to use an If statement, but ive found no other... (2 Replies)
Discussion started by: xzen123
2 Replies

8. Shell Programming and Scripting

pattern matching in case statement

I have a file abc.sh which looks like qacc1 ----> down v5c0 check interface v5c1 I want to read this file line by line and perform a certain action if a particular pattern is found in that line. My code so far looks like this: FileName='abc.sh' while read LINE do echo $LINE case... (2 Replies)
Discussion started by: lassimanji
2 Replies

9. Shell Programming and Scripting

Replacing all except for the matching pattern

Hi all I need to replace all characters in a file except for the matching pattern eg. I need to replace all character with '*' except for the pattern "abc" Input "sdfhgsdf abc ##%$#abcsdfh sdfjkfff" Output "******abc******abc*************" Request for single liner solution (1 Reply)
Discussion started by: raghav288
1 Replies

10. Shell Programming and Scripting

problem with CASE pattern matching

I am using ksh on a HP Ux. I have a simple script but am having problem with the case statement:- #!/usr/bin/sh Chl=”SM.APPLE_SWIFT_DV” LoConfirm=”” case $chl in ) LoConfirm=”Using channel at Building 1” echo “test conditon1” echo $LoConfirm;; ) LoConfirm=”Using... (2 Replies)
Discussion started by: gummysweets
2 Replies
Login or Register to Ask a Question