Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env node
- 'use-strict'
- const process = require('process');
- const fs = require('fs');
- try {
- if (process.argv.length === 3) {
- const stdin = process.openStdin();
- const func = (x) => {
- const str = x.toString('utf-8');
- if (-1 !== str.indexOf(process.argv[2])) {
- process.stdout.write(str);
- }
- };
- stdin.addListener('data', func);
- }
- if (process.argv.length === 4) {
- const exp = process.argv[2];
- const file = fs.readFileSync(process.argv[3], 'utf8');
- let pos = 0;
- let cur = '';
- while (pos < file.length) {
- cur = '';
- while (pos !== -1 && file[pos] !== '\n') {
- cur += file[pos];
- pos++;
- }
- pos++;
- if (-1 !== cur.indexOf(exp)) {
- console.log(cur);
- }
- }
- }
- if (process.argv.length === 5) {
- if (process.argv[2] === '-i') {
- const exp = process.argv[3];
- const file = fs.readFileSync(process.argv[4], 'utf8');
- let pos = 0;
- let cur = '';
- while (pos < file.length) {
- cur = '';
- while (pos !== -1 && file[pos] !== '\n') {
- cur += file[pos];
- pos++;
- }
- pos++;
- if (-1 !== cur.toUpperCase().indexOf(exp.toUpperCase())) {
- console.log(cur);
- }
- }
- }
- if (process.argv[2] === '-n') {
- const exp = process.argv[3];
- const file = fs.readFileSync(process.argv[4], 'utf8');
- let pos = 0;
- let cur = '';
- let now = 1;
- while (pos < file.length) {
- cur = '';
- while (pos !== -1 && file[pos] !== '\n') {
- cur += file[pos];
- pos++;
- }
- pos++;
- if (-1 !== cur.indexOf(exp)) {
- console.log(now + ':' + cur);
- }
- now++;
- }
- }
- if (process.argv[2] === '-e') {
- const exp = process.argv[3];
- const file = fs.readFileSync(process.argv[4], 'utf8');
- file.search(new RegExp(exp));
- }
- }
- if (process.argv.length === 6) {
- if (process.argv[2] === '-m') {
- const lim = process.argv[3];
- const exp = process.argv[4];
- const file = fs.readFileSync(process.argv[5], 'utf8');
- let pos = 0;
- let cur = '';
- let count = 0;
- while (pos < file.length) {
- if (count >= lim) break;
- cur = '';
- while (pos !== -1 && file[pos] !== '\n') {
- cur += file[pos];
- pos++;
- }
- pos++;
- if (-1 !== cur.indexOf(exp)) {
- console.log(cur);
- count++;
- }
- }
- }
- }
- } catch (e) {
- console.error(e.message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement