New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
using sed to replace just the first occurrence of a string #813
Comments
> shell.cat('file.txt');
hello world
hello hello world
> shell.sed(/hello/, 'bye', 'file.txt'); // first occurrence, for each line
bye world
bye hello world
> shell.sed(/hello/g, 'bye', 'file.txt'); // all occurrences, on all lines
bye world
bye bye world To achieve what you want, you can try: var contents = shell.cat('myfile');
var newContents = contents.replace(/(.|\n)*(my pattern)(.|\n)*/, 'replacement text');
(new ShellString(newContents)).to('myfile'); However, a more robust way to solve you specific problem would be: var myPackage = require('package.json');
myPackage.version = 'new version number';
// pretty print with 2 space indent and trailing newline
var packageString = JSON.stringify(myPackage, null, 2) + '\n';
fs.writeFileSync('package.json', packageString); |
Wait, wait.... so reading your last comment: "To achieve what you want, you can try: var contents = shell.cat('myfile'); Your solution to the issue is .... NOT USING "shell.sed" and use "contents.replace" instead? shell.sed doesn't work for this purpose? |
|
So, nothing like this is supported? |
Currently, not supported (not opposed to it, however). Also, that wouldn't necessarily be the right solution for the original post, but that shouldn't deter us from implementing it. |
Hello there
Node version (or tell us if you're using electron or some other framework):
v7.10.0
ShellJS version (the most recent version/Github branch you see the bug on):
v0.8.0
Operating system:
MacOs HighSierra 10.13.2
Description of the bug:
I'm using shelljs to perform a sed command and replace the whole line with a certain occurrence with another text. Here's the code that I'm using:
That version works but it replaces all the occurrence of "version". I have tried this code to replace only the first occurrence:
but that's not working at all: the file remains unchanged.
What am I missing?
The text was updated successfully, but these errors were encountered: