3

I have a file where I want to scan it, find the character '{' and create a new line, then add an IP on the new line and add a semi-cologne to the end of the line, then write to a config file.

I can accomplish this with the following sed command when running from shell:

sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf

inside test.conf:

acl testACL{
};

the output from the command in shell shows:

acl testACL{
1.1.1.1;
};

works perfect! Now the problem is when I get nodejs to execute it:

        var sys = require('sys');
        var exec = require('child_process').exec;
        function puts(error, stdout, stderr) { sys.puts(stdout) };
        exec("sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf",puts);
        return 0;

when I run the command in console: 'nodejs test.js'

I get blank output and when I check the file, the file 'test.conf' has not been altered! why?!

Also if you're thinking 'its a permissions issue!' I've had nodejs exec command write basic echos to the test config file and it worked fine.

I have also tried the 'shelljs' module with no luck there. I have tried countless combinations for hours now with no prevail! I'm puzzled.

1 Answer 1

6

I think you need to escape your \n as \\n. Right now, it's getting parsed as a literal newline in the command, which is messing it up.

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

Comments

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.