How to use the sed function from shelljs
Find comprehensive JavaScript shelljs.sed code examples handpicked from public code repositorys.
shelljs.sed is a method provided by the ShellJS library that performs a search and replace operation on a given file or string.
GitHub: deepraining/blogs
228 229 230 231 232 233 234 235 236 237
// Replace macros in each .js file shell.cd('lib'); shell.ls('*.js').forEach(function (file) { shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file); shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file); shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file); }); shell.cd('..'); // Run external tool synchronously
+ 5 other calls in file
67 68 69 70 71 72 73 74 75 76
const hash = crypto.createHash(algo).update(data, 'utf8').digest('base64') const integrity = `${algo}-${hash}` console.log(`${configPropertyName}: ${integrity}`) sh.sed('-i', new RegExp(`^(\\s+${configPropertyName}:\\s+["'])\\S*(["'])`), `$1${integrity}$2`, configFile) }) }
How does shelljs.sed work?
The shelljs.sed function is used to perform search and replace operations on a given string or file using regular expressions, and returns the modified string or file contents as a string. It takes two or three arguments: the search pattern, the replacement pattern, and an optional path to the file to perform the operation on. If a file path is provided, the operation is performed on the contents of the file and the file is updated with the modified contents. If no file path is provided, the operation is performed on the given string and the modified string is returned.
97 98 99 100 101 102 103 104 105 106
); console.log(file); // replace BN import shell.sed( '-i', 'import BN from "bn.js"', 'import { BN } from "@switchboard-xyz/common"', file
+ 127 other calls in file
132 133 134 135 136 137 138 139 140 141
saveFiles( ['packages/react-native/ReactAndroid/gradle.properties'], tmpVersioningFolder, ); if ( sed( '-i', /^VERSION_NAME=.*/, `VERSION_NAME=${version}`, 'packages/react-native/ReactAndroid/gradle.properties',
+ 4 other calls in file
Ai Example
1 2 3 4
const shell = require("shelljs"); // Replace all occurrences of 'foo' with 'bar' in myfile.txt shell.sed(/foo/g, "bar", "myfile.txt");
This will search for all occurrences of the regular expression /foo/g in myfile.txt and replace them with the string 'bar'.
GitHub: pritamprasd/therapyApp
204 205 206 207 208 209 210 211 212 213
events.emit('log', 'Multiple candidate Java files that extend CordovaActivity found. Guessing at the first one, ' + java_files[0]); } var destFile = path.join(locations.root, 'src', pkg.replace(/\./g, '/'), path.basename(java_files[0])); shell.mkdir('-p', path.dirname(destFile)); shell.sed(/package [\w\.]*;/, 'package ' + pkg + ';', java_files[0]).to(destFile); events.emit('verbose', 'Wrote out Android package name "' + pkg + '" to ' + destFile); if (orig_pkg !== pkg) { // If package was name changed we need to remove old java with main activity
GitHub: lbear9090/wediapp_ionic
262 263 264 265 266 267 268 269 270 271
// interpolate the activity name and package shell.mkdir('-p', activity_dir); shell.cp('-f', path.join(project_template_dir, 'Activity.java'), activity_path); shell.sed('-i', /__ACTIVITY__/, safe_activity_name, activity_path); shell.sed('-i', /__NAME__/, project_name, path.join(project_path, 'res', 'values', 'strings.xml')); shell.sed('-i', /__NAME__/, project_name, path.join(project_path, '.project')); shell.sed('-i', /__ID__/, package_name, activity_path); shell.cp('-f', path.join(project_template_dir, 'AndroidManifest.xml'), manifest_path);
+ 79 other calls in file
GitHub: strong6208/eslint
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
entries.forEach(entry => { assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`); }); // this should result in a changed entry shell.sed("-i", "abc", "xzy", goodFileCopy); fileCache = fCache.createFromFile(cacheLocation, true); assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} is unchanged`); assert(fileCache.getFileDescriptor(goodFileCopy).changed === true, `the entry for ${goodFileCopy} is changed`); });
+ 29 other calls in file
57 58 59 60 61 62 63 64 65 66 67 68
// reset the build number to 0.0.1 shell.sed('-i', /.*"version": ".*",/, ` "version": "0.0.1",`, `${pluginDir}/package.json`); // repeat for package-lock if(shell.test('-e', `${pluginDir}/package-lock.json`)){ shell.sed('-i', /.*"name": ".*",/, ` "name": "${fullPluginName}",`, `${pluginDir}/package-lock.json`); shell.sed('-i', /.*"version": ".*",/, ` "version": "0.0.1",`, `${pluginDir}/package-lock.json`); } // rename the plugin file names
+ 4 other calls in file
GitHub: aklib/laravel-core
121 122 123 124 125 126 127 128 129 130 131 132
} const replaceRecursively = (filename, vendor) => { const original = vendor.src const replacement = `vendors/${vendor.name}/${vendor.filetype}/${basename(vendor.src)}` sh.sed('-i', original, replacement, filename) } const main = () => { const vendors = findVendors()
+ 4 other calls in file
8 9 10 11 12 13 14 15 16 17 18
*/ var fs = require('fs'); var path = require('path'); var sh = require('shelljs'); sh.config.fatal = true; var sed = sh.sed; // Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37 RegExp.quote = function (string) { return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
+ 4 other calls in file
58 59 60 61 62 63 64 65 66 67
const hash = crypto.createHash(algo).update(data, 'utf8').digest('base64') const integrity = `${algo}-${hash}` console.log(`${file.configPropertyName}: ${integrity}`) sh.sed('-i', new RegExp(`^(\\s+${file.configPropertyName}:\\s+["'])\\S*(["'])`), `$1${integrity}$2`, configFile) }) })
+ 2 other calls in file
GitHub: YouRimm/CHIP_U
52 53 54 55 56 57 58 59 60 61
algorithms: ['sha384'] }, data) console.log(`${file.configPropertyName}: ${integrity}`) sh.sed('-i', new RegExp(`(\\s${file.configPropertyName}:\\s+"|')(\\S+)("|')`), `$1${integrity}$3`, configFile) }) })
GitHub: RadioAc/shell-pr
17 18 19 20 21 22 23 24 25 26
// sh.ls('*.js').forEach(function (file) { // /* 这是第一个难点:sed流编辑器,建议专题学习,-i表示直接作用源文件 */ // //将build_version字段替换为'v0.1.2' // sh.sed('-i', 'BUILD_VERSION', 'v0.1.2', file); // //将包含`REMOVE_THIS_LINE`字符串的行删除 // sh.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file); // //将包含`REPLACE_LINE_WITH_MACRO`字符串的行替换为`macro.js`中的内容 // sh.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, sh.cat('macro.js'), file); // });
+ 2 other calls in file
shelljs.exec is the most popular function in shelljs (4615 examples)