Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 4310 days old. Please don't post here. Open a new topic instead.

Recommended Posts

  • Newbies
Posted

Hi, I'm completely new to Groovy and am trying to replace the 3 versions of the double quotes with 2 single quotes in a large CSV file.  I've managed to get the script below working but the input file encoding is UTF8 and the output file comes out as "Western (Mac OS Roman)".

 

Any help would be greatly appreciated, Thanks

File myInFile = new File( myFilePathStart );

myOutFile = new File( myFilePathEnd );

myOutFile.withWriter { out ->

 myInFile.eachLine { line ->

  myOutFileText = line.replaceAll(""", "''");

  myOutFileText = myOutFileText.replaceAll("“", "''");

  myOutFileText = myOutFileText.replaceAll("”", "''");

  myOutFileText = myOutFileText + "n";

  out << myOutFileText;

 }

};

return true;

Posted

myInFile = new File( myFilePathStart )
myOutFile = new File( myFilePathEnd )

try{
myOutFile.withWriter('UTF-8') { out ->
 myInFile.eachLine { line ->
  myOutFileText = line.replace(""", "''")
.replace("“", "''")
.replace("”", "''") + 'n'
  out << myOutFileText
 } //end eachLine
} //end writer
} catch (e){
 return e
}
return true

  • Newbies
Posted

Thanks "enthusiast with a bit of geek"... that almost worked.  I just had to add the UTF8 encoding to the "myInFile.eachLine" command also.  The script now looks like the following and does exactly what I need;

 

 

myInFile = new File( myFilePathStart )
myOutFile = new File( myFilePathEnd )
 
try{
   myOutFile.withWriter('UTF-8') { out ->
      myInFile.eachLine('UTF-8') { line ->
         myOutFileText = line.replaceAll(""", "''")
         .replaceAll("“", "''")
         .replaceAll("”", "''") + "n"
        out << myOutFileText;
      } //end eachLine
   } //end writer
} catch (e) {
   return e
}
return true

This topic is 4310 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.