Skip to content

Instantly share code, notes, and snippets.

@yorkie
Created June 19, 2014 06:02
Show Gist options
  • Save yorkie/03f2ddb77eef9ed76323 to your computer and use it in GitHub Desktop.
Save yorkie/03f2ddb77eef9ed76323 to your computer and use it in GitHub Desktop.
A simple javascript script to help you take the **awk** main loop
// This simple javascript script what @yorkie write is just show you my
// knowledge on awk main loop.
var fs = require('fs');
var lines = fs.readFileSync('your awk file path').toString('utf8').split('\r\n');
var rules = [
// Here you define the rule for input
]
lines
.filter(function(line) {
return rules.filter(function (rule){
// match rule for this line and returns result
return rule.match(line);
}
})
.filter(function(res) {
// if result isn't an array or its length is less than zero(0), drop it.
return Array.isArray(res) && res.length > 0;
});
// Then, the processing of awk main loop just is:
// 1. load the rules from you specified awk script
// 2. load the input from file or shell and read it line by line
// 3. for one line, it apply all the rules loaded from the step 1, and selected the proper line.
// 4. just output the result that passed the below 3 steps.
@yorkie
Copy link
Author

yorkie commented Jun 19, 2014

You could visit http://docstore.mik.ua/orelly/unix2.1/sedawk/ch07_04.htm to get more details about AWK programming language, and sed the other text-processing tool in *nix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment