0

Is it possible to read a text file and replace a character or strings and save it in a new file using CasperJs? I tried searching a lot but I can only find reading and writing data in text files...


My main aim is to fetch some text and store it in json file, but the problem is I am getting \n with the text.

I can fetch the text and store it in a json file, but to remove that \n character, I thought of opening that file and replace "\n" with null.

text to be fetched:

hello
abc

I am using these commands:

var a = this.fetchText({type: 'xpath', path: 'tr5'});
this.echo(JSON.stringify(a));   

Output:

hello\n
abc

and I don't want this \n character. I want to print "hello" and "abc" in same line.

Expected:

hello abc

Please help me with the code.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Abhishek
  • 1
  • 1
  • You mean files in the file system? There is no other way besides the PhantomJS fs module. What is the problem exactly? – Artjom B. Aug 18 '14 at 12:53
  • I don't think CasperJS is the right tool for the job. If you could expand on what you're trying to achieve we might be able to help point you in the right direction. – OldTinfoil Aug 18 '14 at 12:53
  • possible duplicate of [How can I replace newlines/line breaks with spaces in javascript?](http://stackoverflow.com/questions/9849754/how-can-i-replace-newlines-line-breaks-with-spaces-in-javascript) – Artjom B. Aug 19 '14 at 09:58

1 Answers1

0

so Phantomjs has the FS module this is capable of reading and writing. So to answer your question yes casper uses the fs module to read and write. If you need casper to open a file and read line by line you can do that. Casper is all JavaScript. So if you want to parse a file line by line remove certain characters, add characters etc... all of that can be done using CasperJS.

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • How do i edit and remove certain characters? Can you help me with the code? I need to remove "\n" character – Abhishek Aug 19 '14 at 04:58
  • You'll probably need some practice with JavaScript if you're asking me this. but here is a link to an answer for your question. http://stackoverflow.com/questions/10805125/how-to-remove-all-line-breaks-from-a-string – Chris Hawkes Aug 19 '14 at 13:37