XML Copy & replace problem


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

Your original awk works just great:

awk -F'"' '
NR==1 {
OFS=FS;
split(FILENAME, x, ".");
prefix=toupper(x[1]"C_");
}
/[Cc][Oo][Uu][Nn][Tt][Yy]/ {
$6=prefix $4;
}
{
print
}' FILENAME_GOES_HERE

My mistake was not realizing that the filename to be processed goes at the bottom of the script.
I thought it was the output from the print command.Smilie

The problem now is, how do I pipe several thousand files through the awk?

As you can see I've tried several ways to no avail.

Again, all your help is greatly appreciated.
# 9  
Old 06-18-2008
Quote:
Originally Posted by xenixuser
for i in ls *.xml; do
cat `$i` | awk -F'"' '
The loop and the backticks look wrong. Try this variation:

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

You were overwriting the temp file on each iteration; my guess is that it simply didn't print anything on the last round, so you ended up with an empty result file. Putting the redirection after the done should hopefully work better.

Actually you probably don't even need the loop; try awk '... script goes here ...' *.xml >temp

Last edited by era; 06-18-2008 at 03:52 PM.. Reason: Surely, awk can cope with more than one input file
# 10  
Old 06-18-2008
era is right. If you try this script you don't even need to redirect the output to a temp file: awk does all the work for you:

Code:
rm *.tmp

awk -F'"' '
   FNR==1 {
      OFS=FS;
      split(FILENAME, x, ".");
      prefix=toupper(x[1]) "C_";
      tmpfile=FILENAME ".tmp";
   }
   /<sub_unit / {
      $6=prefix $4;
   }
   {
      print >> tmpfile;
   }
' *.xml

This creates a "*.tmp" file for each input file.
# 11  
Old 06-18-2008
Thanks era,

will give it a try and let you know.
# 12  
Old 06-18-2008
Robotronic

Looks good.

Will give it a whirl and let you know.
# 13  
Old 06-18-2008
Quote:
Originally Posted by robotronic
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.
Oh, by the way, since this seem no more a Cygwin problem, I may had encountered this issue with Windows Services for UNIX, not Cygwin Smilie
# 14  
Old 06-18-2008
Robotronic

I just copy & pasted it into a file and tried to run in under Cygwin
and on my test files here at home it worked perfectly.Smilie

Thanks again for all your efforts, I really appreciate the assistance.
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