February 14, 201313 yr Newbies 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;
February 14, 201313 yr 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
February 15, 201313 yr Author Newbies Solution 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
Create an account or sign in to comment