【问题标题】:How to print lines that contains specific string?如何打印包含特定字符串的行?
【发布时间】:2017-03-31 16:47:39
【问题描述】:

我想打印字符串中包含日期的行,我正在使用拆分模块来完成该任务。下面的代码总是打印 else 语句。

ctrl.js

fs.readFile(dir + '/' + logFile, 'utf8', function(err, data) {
        var lines = data.split('\n');
        var linesWithDate = lines.split('|')[0].replace(/[\[\]']+/g,'');
        lines.forEach(function(line) {
            if (linesWithDate) {
                console.log('print lines with date',line);
             } else {
                console.log('print lines without date',line);
             }
        }
    });

文件数据

[2017-03-23T18:13:16Z]|zlpv7490|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event lorem ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy 
[2017-03-23T19:20:16Z]|zlpv7490|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event lorem ipsum

【问题讨论】:

  • 你试过正则表达式吗?这是正则表达式的 JavaScript 文档的链接。 developer.mozilla.org/ro/docs/Web/JavaScript/Guide/…
  • 仅使用 grep 是否存在问题,至少 40 年来它已经能够在睡眠中做到这一点。
  • @torazaburo 我从未使用过 grep 任何参考或示例?
  • 嗯,谷歌“grep”。
  • 我也有合并任务,如果第二行没有日期与行之前的合并,这是最终目标,不确定 grep 是否也有助于实现这一目标。

标签: javascript node.js regex


【解决方案1】:

如何打印包含特定字符串的行?

const split = require('split');
fs.createReadtStream(path.join(dir, logFile), 'utf8')
  .pipe(split()).on('data', (line) => {
    if (line.indexOf('string') > -1) {
        console.log('Line with string:', line);
    } else {
        console.log('Line without string:', line);
    }
});

我想打印字符串中包含日期的行,我正在使用拆分模块来完成该任务。下面的代码总是打印 else 语句

我认为您没有使用split 模块。这个例子是:

const split = require('split');
const regex = require('regex-iso-date');
fs.createReadtStream(path.join(dir, logFile), 'utf8')
  .pipe(split()).on('data', (line) => {
    if (regex().test(line)) {
        console.log('Line with date:', line);
    } else {
        console.log('Line without date:', line);
    }
});

请注意,这不一定是有效日期,因为它可能与 2017 年 13 月 13 日之类的日期匹配... - 要测试有效日期,请仅查看此答案:

或者,如果您想匹配您的特定字符串,例如 [2017-03-23T18:13:16Z],那么您可以尝试这样的操作:

const split = require('split');
const regex = /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\w+\]/;
fs.createReadtStream(path.join(dir, logFile), 'utf8')
  .pipe(split()).on('data', (line) => {
    if (regex.test(line)) {
        console.log('Line with date:', line);
    } else {
        console.log('Line without date:', line);
    }
});

请注意,如果您的文件中有无效日期,它也会匹配无效日期。

【讨论】:

  • 我可以使用拆分包来完成这个任务让我看看
  • 我使用 split 和 regex 模块向我的代码添加了第二个答案,它只打印 console.log('Line without date:', line) 它打印所有行
  • @hussain 正则表达式可能过于严格。见第三个例子。我更新了我的答案。
  • 所以在第三个答案中,正则表达式将不再起作用,它会抛出异常
  • @hussain 是的,只需删除括号。请参阅我的更新答案。
【解决方案2】:

这里有一些代码,它只按行打印出有效的解析日期。我使用 readline 模块而不是 readfile,但您应该能够通过替换 input: process.stdin 轻松调整逻辑: '使用严格';

var sExample = '[2017-03-23T18:13:16Z]|zlpv7490|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event lorem ipsum';

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.setPrompt('Enter example string to test > ');
rl.prompt();

rl.on('line', (line) => {
  var sCandidate = line.split('|')[0].replace(/[\[\]']+/g,'');

  if (Date.parse(sCandidate)) {                   //it is a valid date
    console.log(sCandidate);
  }

  process.exit(0);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2019-04-16
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多