Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 从文本文件中提取字符串_Batch File_Cmd_Findstr - Fatal编程技术网

Batch file 从文本文件中提取字符串

Batch file 从文本文件中提取字符串,batch-file,cmd,findstr,Batch File,Cmd,Findstr,我想在windows中通过批处理从中等大小的纯文本文件(比如test.txt)中提取字符串集。所以当我这样做的时候: findstr "ssh-rsa" test.txt >test.txt 我得到与输出相同的test.txt。如何解决这个问题 编辑1:这是最新版本,我需要在第522行搜索文本,或者更具体地说75:d9:e3:5b:c8:17:ef:72:92:78:e5:8e:0c:82:7e:e1 编辑2:来自外部(可变)源: 第522行是第二个起始“ec2:2048”-发布的代码

我想在windows中通过批处理从中等大小的纯文本文件(比如test.txt)中提取字符串集。所以当我这样做的时候:

findstr  "ssh-rsa" test.txt >test.txt
我得到与输出相同的test.txt。如何解决这个问题

编辑1:这是最新版本,我需要在
第522行搜索文本,或者更具体地说
75:d9:e3:5b:c8:17:ef:72:92:78:e5:8e:0c:82:7e:e1

编辑2:来自外部(可变)源:


第522行是第二个起始“ec2:2048”-发布的代码部分是第514..525行

插入新行不是一个选项,因为您首先需要进行搜索,以确保不会打断您试图查找的字符串-第22条军规

只要文件大小小于2G字节,就可以使用my-a混合JScript/批处理脚本,对文本文件执行正则表达式搜索和替换。REPL.BAT是纯脚本,从XP开始在任何Windows机器上本机运行

您还没有说明确切的搜索词是什么,所以我将只搜索
ssh rsa xx:nn:nn
。我使用
\JMATCH
选项将每个匹配项放在新行上,并丢弃所有不匹配项

jrepl "ssh-rsa [a-z][a-z]:\d\d:\d\d:\d\d" "$0" /jmatch /f test.txt /o result.txt
如果要覆盖原始文件,则

jrepl "ssh-rsa [a-z][a-z]:\d\d:\d\d:\d\d" "$0" /jmatch /f test.txt /o -

如果在批处理脚本中使用,不要忘记使用
调用jrepl…
,因为jrepl也是一个批处理脚本。

引人注目的问题:如何解析结构不好的文本文件。这是我的纯批量解决方案,尽管Magoo给出了令人信服的警告:

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
:: define a keyword
set "keyword=ssh-rsa"
:: create output file
type nul>testtest.txt
:: process input file line by line  
for /F "tokens=* delims=" %%F in ('findstr /I "%keyword%" test.txt') do ( 
  call :fooProc "%keyword%" "%%~F"
)
:: show result
type testtest.txt
ENDLOCAL
goto :eof

:: Procedure seeks for %~1 keyword in %~2 line and
:: for each found keyword writes the pair 'keyword value' 
:: to given output file, where 'value' = word next to 'keyword'.
:: Structure of 'line' supplied considered not well defined:
:: there could appear 0..n irrelevant words between
:: near-by 'keyword value' pairs and/or afore of first one
:: and/or by last one.
:: Suppose no presence of non-printable characters, or characters
:: with special meaning to batch processing (echo, call, if, ...)
:: like !, % or |, >, >>, < redirectors in the 'line' supplied.  
:fooProc
  SETLOCAL enableextensions enabledelayedexpansion
  set "foo=%~2"
:fooLoop
  for /F "tokens=1,2* delims= " %%G in ("!foo!") do ( 
    if /I "%%~G"=="%~1" (
      @echo %%G %%H>>testtest.txt
      set "foo= %%I"
    ) else (
      @rem echo         forgotten %%G
      set "foo=%%H %%I"
    )
  )
  if not "!foo!"==" " goto :fooLoop
  ENDLOCAL
goto :eof
:: end fooProc
@ECHO关闭
SETLOCAL
设置“找到\u ssh\u rsa=”
对于/f“tokens=1-5delims=“%%a IN(q27546207.txt)DO(
如果已定义,则找到\u ssh\u rsa(
如果“%%a”==”ec2:“如果“%%e”==”(RSA)“设置”数据=%%c”
)否则(
如果“%%b”==“ssh rsa”设置为“找到”\u ssh\u rsa=Y”
)
)
找到的回显数据为“%data%”
后藤:EOF
我使用了一个名为
q27546207.txt
的文件,其中包含用于测试的数据


您说您想要第522行的
75:d9:e3:5b:c8:17:ef:72:92:78:e5:8e:0c:82:7e:e1
,但您指定的信号是
ssh rsa
。我假设您希望数据位于
ssh rsa
之后的一行,该行以
ec2:
开头,以
(rsa)
结尾,因为缺少具体信息。

您的问题太模糊,甚至不可能给出答案。你所说的“中等大小”文件(标题)或“长文件”(叙述)是什么意思?关于这个文件你还能透露什么?是否有某些字符肯定不会出现在文件中?这是唯一出现的模式“ssh rsa”后跟12个字符(总是12个?)文件是否包含不可打印的字符,或者对
cmd
like()*%有特殊意义的字符+“;~您打算如何“任意插入换行符”"? 你能使用像SED这样的免费软件吗?@rojo正如问题所述,我在寻找的是windows解决方案,而不是shell!我不确定文件中将显示的字符类型,因为它是EC2实例的系统日志。所需的确切字符串将是这种形式,
ssh rsa c1:e8:5c:66:c2:b0:6d:68:a7:94:fd:05:4a:26:79:b2
,正如@dbenham在他的回答中所说,我不能随意插入换行符,因此不打算使用该选项。无论如何,这是个坏主意!不,我不能使用任何固件实用程序。对不起,你没有意义。首先,您说您的源文件不包含换行符,但您发布了一个充满换行符的示例文件,并说您需要在第522行进行搜索。您也没有充分描述所要查找的字符串的规则。如果您对如何识别所需字符串给出了精确的描述,那么也许有人可以提供帮助。仅仅为一个特定的例子说明结果是不好的-如果你知道结果,那么为什么要搜索呢?您需要说明如何识别所有情况下的字符串的精确通用规则。所需的确切字符串将采用以下形式:,
ssh rsa c1:e8:5c:66:c2:b0:6d:68:a7:94:fd:05:4a:26:79:b2
请给我REPL.bat的链接另一个示例搜索字符串是
ssh rsa 9a:36:b8:0a:2c:9a:0c:99:49:48:72:bd:dd:31:2d:df
。所以它可以是字母和数字的任意组合,但大小是固定的!我看到了REPL.BAT,它就在JREPL.BAT的下方。我的要求也迫使我只使用纯批处理文件,所以请您提出一个解决方案,并记住上面评论中所述的所有其他事实。这是github的文件链接[我需要搜索第522行的文本,或者更具体地说,
75:d9:e3:5b:c8:17:ef:72:92:78:e5:8e:0c:82:7e:e1
@SubhamTripathi-REPL.BAT没有所需的功能。我放弃了REPL.BAT的开发,转而使用JREPL.BAT。本机批处理命令无法合理地搜索不包含换行的大型文件,因为批处理无法处理长度超过8191字节的字符串。解决方案可以使用FC以二进制模式编写,每次搜索一个字节,但这完全是不切实际的,而且速度很慢-只适合作为学术练习。我只是将您的
github
帖子中的数据复制/粘贴到提到的文件中。如果这样做会发生什么?我不知道理解你的评论
@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
:: define a keyword
set "keyword=ssh-rsa"
:: create output file
type nul>testtest.txt
:: process input file line by line  
for /F "tokens=* delims=" %%F in ('findstr /I "%keyword%" test.txt') do ( 
  call :fooProc "%keyword%" "%%~F"
)
:: show result
type testtest.txt
ENDLOCAL
goto :eof

:: Procedure seeks for %~1 keyword in %~2 line and
:: for each found keyword writes the pair 'keyword value' 
:: to given output file, where 'value' = word next to 'keyword'.
:: Structure of 'line' supplied considered not well defined:
:: there could appear 0..n irrelevant words between
:: near-by 'keyword value' pairs and/or afore of first one
:: and/or by last one.
:: Suppose no presence of non-printable characters, or characters
:: with special meaning to batch processing (echo, call, if, ...)
:: like !, % or |, >, >>, < redirectors in the 'line' supplied.  
:fooProc
  SETLOCAL enableextensions enabledelayedexpansion
  set "foo=%~2"
:fooLoop
  for /F "tokens=1,2* delims= " %%G in ("!foo!") do ( 
    if /I "%%~G"=="%~1" (
      @echo %%G %%H>>testtest.txt
      set "foo= %%I"
    ) else (
      @rem echo         forgotten %%G
      set "foo=%%H %%I"
    )
  )
  if not "!foo!"==" " goto :fooLoop
  ENDLOCAL
goto :eof
:: end fooProc
ssh-rsa ab:23:45:01 abc ssh-rsa ab:23:45:02 qwe rtz ssh-rsa ab:23:45:03 sedr fdert ssh-rsa ab:23:45:04 ssh-rsa ab:23:45:05 end01 end02 end03
2ndline ssh-rsa cd:23:45:21 abc 222 SSH-RSA cd:23:45:22 qwe rtz ssh-rsa cd:23:45:23 sedr fdert SSH-RSA cd:23:45:24 ssh-rsa cd:23:45:25 end21 end22 end23
3rdline ssh-rsa ef:23:45:31 333 cde ssh-rsa ef:23:45:32 qwe rtz ssh-rsa ef:23:45:33 sedr fdert ssh-rsa ef:23:45:34 ssh-rsa ef:23:45:35