从批处理脚本文本文件中读取特定的Word(2号线,3字) [英] Read specific Word(Line 2, Word 3) from a text file by batch script

查看:589
本文介绍了从批处理脚本文本文件中读取特定的Word(2号线,3字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从通过​​批处理脚本文本文件中读取特定的单词。说我要行2字3被读取。

下面是code:

  @ECHO OFF
    CLS
    SET /普线=请输入行号:
    进入ECHO行号:%行%    SET / p字=请输入字数:
    进入ECHO字号码是:%字%
    FOR / F令牌=%字%跳跃=%行%delims =%% G IN(E_dir.txt)DO呼应选的词是:%%摹

问题是,这给了我所有的话3号线从2开始(例如,用户输入的是1号线,3字)。有一个与行数有问题,但是我可以搞定。我的问题是它给我所有的话时,我只需要2号线的单个字和字3。

输入文件: E_Dir.txt

 月,雪,02
二月,多雨,15
三月,阳光明媚,25
四月,测试20

输出:

 请输入号码:1
进入行号:1
请输入字数:3
进入字编号为:3
选择的字是:15
选择的字是:25
选择的字是:20

期望输出:

 请输入号码:1
进入行号:1
请输入字数:3
进入字编号为:3
选择的字是:15


解决方案

路第一
...

  FOR / F令牌=%字%跳跃=%行%delims =%% G IN(E_dir.txt)DO呼应选的词是:%% G&安培;转到nextline
:nextline

办法第二
...

 集SHOWME = Y
FOR / F令牌=%字%跳跃=%行%delims =%% G IN(E_dir.txt)做,如果定义设置SHOWME SHOWME =安培;回声选上的话就是:%%摹


修改

  SET / A SHOWME =行1
SET SHOWME =跳过=%SHOWME%
IF%行%EQU 1套SHOWME =
FOR / F令牌=%字%SHOWME%%delims =%% G IN(E_dir.txt)做,如果定义设置SHOWME SHOWME =安培;回声选上的话就是:%%摹

请注意,这里的%行% 1 SHOWME 是故意设置的空间。这确保了 SHOWME 具有价值进行初始。

我不知道你在哪里流浪2的来源。该程序为我工作。也许你可能想用十六进制编辑器查看的资源文件。它可以具有没有低频的线。也可能检查您的批次。如果你已经使用记事本,尝试像EDITPLUS或记事本编辑+ +。记事本做一些奇怪的事情。

I need to read specific words from a text file through batch script. Say I want Line 2 word 3 to be read.

Here is the code::

   @ECHO OFF
    cls
    SET /P line=Please enter Line number: 
    ECHO Line number entered is : %line%

    SET /P word=Please enter Word number: 
    ECHO Word number entered is : %word%


    FOR /F "tokens=%word% skip=%line% delims=," %%G IN (E_dir.txt) DO echo Chosen word is: %%G

The problem is, this gives me all the 3rd words starting from line 2. (say, user input is line 1, Word 3). There is a problem with the line count, But I can handle that. My problem is it is giving me all the words when I need only the single word of Line 2 and word 3.

Input file: E_Dir.txt

January,Snowy,02
February,Rainy,15
March,Sunny,25
April,Test,20

Output:

Please enter Line number: 1
Line number entered is : 1
Please enter Word number: 3
Word number entered is : 3
Chosen word is: 15
Chosen word is: 25
Chosen word is: 20

Expected Output:

Please enter Line number: 1
Line number entered is : 1
Please enter Word number: 3
Word number entered is : 3
Chosen word is: 15

解决方案

Way the first ...

FOR /F "tokens=%word% skip=%line% delims=," %%G IN (E_dir.txt) DO echo Chosen word is: %%G&goto nextline
:nextline

Way the second ...

set showme=Y
FOR /F "tokens=%word% skip=%line% delims=," %%G IN (E_dir.txt) DO if defined showme set showme=&echo Chosen word is: %%G


Amendment

SET /a showme=line-1
SET showme=skip=%showme%
IF %line% equ 1 set "showme= "
FOR /F "tokens=%word% %showme% delims=," %%G IN (E_dir.txt) DO if defined showme set showme=&echo Chosen word is: %%G

Note that where %line% is 1, showme is deliberately set to a SPACE. This ensures that showme has a value initially.

I have no idea where your stray "2" is coming from. The procedure worked for me. Perhaps you may want to check the sourcefile with a hex editor. It may have a line without a LF. Possibly also check your batch. If you've used NOTEPAD, try an editor like EDITPLUS or NOTEPAD++. Notepad does some strange things.

这篇关于从批处理脚本文本文件中读取特定的Word(2号线,3字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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