Shell scripts in Javascript.

I’m a big fan of javascript.

My first taste of javascript was during a bout of insomnia when I wrote a little night light sleeping aid that replaced a 50$ gadget. Then came real time synthesis of chimes, then face recognition in the browser.

The one thing that I missed was being able to do shell scripting. I used to use sed, awk and python and even perl for this purpose depending on complexity of the task. Now I use javascript.

Replicating sed with Javascript and node.js – a Javascript shell script template

For the purpose of illustration let’s replicate the most basic and useful functionality of sed.

Here is our test text:



The two most important days in your life are the day you are born and the day you find out why.

― Mark Twain

The javascript example below does the same as the following sed statement.

sed 's/day/night/g' someTextFile.txt

It replaces every instance of “day” with “night”. Here is an example of what it does:

$ echo "The two most important days in your life are the day you are born and the day you find out why." > MarkTwianQuote.txt $ sed 's/day/night/g' MarkTwianQuote.txt The two most important nights in your life are the night you are born and the night you find out why.

Basic javascript shell script for replacing strings (requires node.js):

#! /usr/local/bin/node // Shebang to tell bash to use node. var fs = require('fs'); // For reading files. var args = process.argv.slice(2); // Get the command line arguments. var theFileToProcess = args[0]; var theRegexp = eval(args[1]); var theReplacementString = args[2]; fs.readFile(theFileToProcess, "ascii", function (err, data) { if (err) { console.error(err); process.exit(1); } // Split file on linebreaks. lineArr = data.trim().split("n"); // Loop over every line. lineArr.forEach(function (line) { // Find all instances of regexp and replace. var result = theRegexp.exec(line); if (result !== null) { var newLine = line.replace(theRegexp, theReplacementString); console.log(newLine); } else { console.log(line); } }); });

Now if you save the above script as replace.js, and run this on the command line you get:

$ echo "The two most important days in your life are the day you are born and the day you find out why." > MarkTwianQuote.txt $ ./replace.js MarkTwianQuote.txt /day/g night The two most important nights in your life are the night you are born and the night you find out why.

Processing large files

The above example takes the whole file into memory. If you have large files you will run out of memory with the above method.

To process a file that does not fit in memory, you can use the module line-reader which allows you to read a file line by line.

#! /usr/local/bin/node // For reading large files line by line. Install with nmp install line-reader. var lineReader = require('line-reader'); var args = process.argv.slice(2); // Get the command line arguments. var theFileToProcess = args[0]; var theRegexp = eval(args[1]); var theReplacementString = args[2]; lineReader.eachLine(theFileToProcess, function(line, last) { // Find all instances of regexp and replace. var result = theRegexp.exec(line); if (result !== null) { var newLine = line.replace(theRegexp, theReplacementString); console.log(newLine); } else { console.log(line); } });