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.

3
  • You mean files in the file system? There is no other way besides the PhantomJS fs module. What is the problem exactly? Commented Aug 18, 2014 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. Commented Aug 18, 2014 at 12:53
  • possible duplicate of How can I replace newlines/line breaks with spaces in javascript? Commented Aug 19, 2014 at 9:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How do i edit and remove certain characters? Can you help me with the code? I need to remove "\n" character
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. stackoverflow.com/questions/10805125/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.