Advertisement
Void-voiD

Untitled

Dec 21st, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #! /usr/bin/env node
  2. 'use-strict'
  3.  
  4. const process = require('process');
  5. const fs = require('fs');
  6.  
  7. try {
  8. if (process.argv.length === 3) {
  9. const stdin = process.openStdin();
  10. const func = (x) => {
  11. const str = x.toString('utf-8');
  12. if (-1 !== str.indexOf(process.argv[2])) {
  13. process.stdout.write(str);
  14. }
  15. };
  16. stdin.addListener('data', func);
  17. }
  18. if (process.argv.length === 4) {
  19. const exp = process.argv[2];
  20. const file = fs.readFileSync(process.argv[3], 'utf8');
  21. let pos = 0;
  22. let cur = '';
  23. while (pos < file.length) {
  24. cur = '';
  25. while (pos !== -1 && file[pos] !== '\n') {
  26. cur += file[pos];
  27. pos++;
  28. }
  29. pos++;
  30. if (-1 !== cur.indexOf(exp)) {
  31. console.log(cur);
  32. }
  33. }
  34. }
  35. if (process.argv.length === 5) {
  36. if (process.argv[2] === '-i') {
  37. const exp = process.argv[3];
  38. const file = fs.readFileSync(process.argv[4], 'utf8');
  39. let pos = 0;
  40. let cur = '';
  41. while (pos < file.length) {
  42. cur = '';
  43. while (pos !== -1 && file[pos] !== '\n') {
  44. cur += file[pos];
  45. pos++;
  46. }
  47. pos++;
  48. if (-1 !== cur.toUpperCase().indexOf(exp.toUpperCase())) {
  49. console.log(cur);
  50. }
  51. }
  52. }
  53. if (process.argv[2] === '-n') {
  54. const exp = process.argv[3];
  55. const file = fs.readFileSync(process.argv[4], 'utf8');
  56. let pos = 0;
  57. let cur = '';
  58. let now = 1;
  59. while (pos < file.length) {
  60. cur = '';
  61. while (pos !== -1 && file[pos] !== '\n') {
  62. cur += file[pos];
  63. pos++;
  64. }
  65. pos++;
  66. if (-1 !== cur.indexOf(exp)) {
  67. console.log(now + ':' + cur);
  68. }
  69. now++;
  70. }
  71. }
  72. if (process.argv[2] === '-e') {
  73. const exp = process.argv[3];
  74. const file = fs.readFileSync(process.argv[4], 'utf8');
  75. file.search(new RegExp(exp));
  76. }
  77. }
  78. if (process.argv.length === 6) {
  79. if (process.argv[2] === '-m') {
  80. const lim = process.argv[3];
  81. const exp = process.argv[4];
  82. const file = fs.readFileSync(process.argv[5], 'utf8');
  83. let pos = 0;
  84. let cur = '';
  85. let count = 0;
  86. while (pos < file.length) {
  87. if (count >= lim) break;
  88. cur = '';
  89. while (pos !== -1 && file[pos] !== '\n') {
  90. cur += file[pos];
  91. pos++;
  92. }
  93. pos++;
  94. if (-1 !== cur.indexOf(exp)) {
  95. console.log(cur);
  96. count++;
  97. }
  98. }
  99. }
  100. }
  101. } catch (e) {
  102. console.error(e.message);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement