如果包含单词,则打印行 [英] Print line if contains word

查看:59
本文介绍了如果包含单词,则打印行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个批处理文件来为我执行一些操作.
现在,我确实了解几种编程语言,但是在批处理文件编程方面,我还没有做太多事情.

I am trying to make a batch file to execute a few things for me.
Now I do understand a few programming languages, but I haven't done much in batch file programming yet.

我从简单开始.我需要一个批处理文件来读取文件并逐行读取它.
如果该行包含单词"node",我希望在命令提示符下打印该行.

I am starting simple. I need a batch file to read in a file and read through it line by line.
If the line contains the word "node" I would like to have the line printed in my command prompt.

file1 =我读取的文件是由用户提供的.
该部分有效.

file1 = the file I read in that was given by the user.
That part works.

for /f "tokens=*" %%l in (%file1%) do (
    REM Find the lines that contain: node
    if exist "node" in %%l (
        echo %%l
    )
    REM for %%i in ('findstr "node" %%l') do (
        REM echo %%l
    REM )
)

我注释掉的部分也尝试过.
但是他们两个都不起作用.
它会一直返回所有行.

The part that I commented out I also tried.
But both of them don't work.
It stil return all lines the whole time.

任何人都可以给我提示我做错了什么吗?

Can anyone give me a hint on what I am doing wrong?

推荐答案

如果您只是想在不进行逻辑检查的情况下输出匹配行,请执行

If you simply want to output the matching line with no logic checking, do

findstr /i "node" "%file1%"

如果要在找到该行的情况下执行一些代码,请使用条件执行.

If you want to execute some code if the line is found, use conditional execution.

findstr /i "node" "%file1%" && (

    rem File contained "node".  Do some stuff.

) || (

    rem File did not contain "node".  Do something else.

)

如果您想使用 findstr 来测试字符串的存在而无需将结果实际转储到控制台,只需在之前或之后添加> NUL findstr 命令.

And if you want to use findstr to test for the existence of a string without actually dumping the result to the console, simply add >NUL either before or after the findstr command.

>NUL findstr /i "node" "%file1%" && (success) || fail

这篇关于如果包含单词,则打印行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆