简体   繁体   English

从文本文件中批量获取特定字符串

[英]Get a specific string from a text file in batch

I looked for a long time and didn't find the exact answer to my question. 我看了很长时间,没有找到问题的确切答案。 I'm kinda new to batch so maybe I didn't understand but here's my problem: 我是一批新手,所以也许我听不懂,但这是我的问题:

I use a batch file to do a sql query toward a remote oracle database with: 我使用批处理文件通过以下方式对远程oracle数据库执行sql查询:

@echo @ScriptToExecute.sql | sqlplus username/password@database > result.txt

The answer is written into result.txt, it looks like this: 答案将写入result.txt,如下所示:

-- assumed empty --
SQL*Plus: Release 9.2.0.1.0 - Production on Je Avr 27 12:34:34 2017

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.


Connecté à :
Oracle9i Release 9.2.0.6.0 - Production
JServer Release 9.2.0.6.0 - Production

SQL>
  COUNT(*)
----------
        13

SQL>Déconnecté de Oracle9i Release 9.2.0.6.0 - Production
JServer Release 9.2.0.6.0 - Production

What I want is to open this text file and get into a variable the number 13. The purpose of this is of course to execute the batch file once every day to make sure the answer is still 13. In the end my batch file must return the number of the answer to my query. 我要打开的文本文件并输入数字13。这当然是每天执行一次批处理文件,以确保答案仍然是13。最后,我的批处理文件必须返回我的查询答案的编号。

The answer I want is in the line number 14. 我想要的答案在第14行。

Any thoughts ? 有什么想法吗 ?

Thank you for your reply 谢谢您的回复

edit: I tried a lot of things like using a powershell command to use head and tail command I used to know in shell but I failed to use powershell. 编辑:我尝试了很多事情,例如使用powershell命令来使用我以前在shell中知道的head和tail命令,但是我无法使用powershell。 I also tried to use sqlcmd which can filter the results but it has to be installed and I can't install anything on the different hosts. 我也尝试使用sqlcmd可以过滤结果,但是必须安装它,并且不能在其他主机上安装任何东西。 I tried using the command more +13 this way: 我尝试使用命令more +13这样:

@echo @ScriptToExecute.sql | sqlplus username/password@database > more +13 result.txt

But it didn't work either... 但这也不起作用...

Once you've created the file result.txt 创建文件result.txt

set "numreturned="
for /f %%a in (result.txt) do if "%%a"=="----------" (
 set "numreturned=%%a"
 ) else if defined numreturned set /a numreturned=%%a&goto foundit
:foundit
echo number returned=%numreturned%

OR, if you don't want to create result.txt , 或者,如果您不想创建result.txt

set "numreturned="
for /f %%a in (
 'echo ScriptToExecute.sql ^| sqlplus username/password@database'
) do if "%%a"=="----------" (
 set "numreturned=%%a"
 ) else if defined numreturned set /a numreturned=%%a&goto foundit
:foundit
echo number returned=%numreturned%

Note that execution directly from the prompt and as lines with a batch file are different. 请注意,直接从提示符处执行和带有批处理文件的行是不同的。 The metavariable (loop-control variable) %%x must be referenced as %%x for a batch line and %x if executed from the command prompt. 所述metavariable (循环控制变量) %%x必须以引用%%x为一批线和%x如果从命令提示来执行。 Since it makes little sense to repeatedly execute a line containing a for from the prompt (it's easier to create a batch file), I post the batch-file version. 由于从提示重复执行包含for的行几乎没有意义(创建批处理文件更容易),因此我发布了批处理文件版本。 User's responsibility to adjust for direct-from-prompt if desired. 如果需要,用户有责任根据提示进行直接调整。

Having set numreturned to nothing (ie not defined) 设置numreturned (即未定义)

Read the file to %%a , selecting just the first token using spaces as a delimiter (default for tokens and delims) 将文件读取到%%a ,仅使用空格作为分隔符选择第一个标记(标记和delims的默认值)

When the first line of - appears (there are 10 - characters), set numreturned to some value so that it is defined. 当第一线的-出现(有10 -字符),组numreturned为某个值,使得它被定义。

When the next line arrives, it will not be a line of - , but numreturned is now defined, so assign the data from that line (the count) and terminate the loop by going to the label :foundit 当在下一行到达时,它不会是一个线-numreturned现在定义,所以从分配给行(计数)的数据,并通过进入标签终止循环:foundit

Then analyse the value found and do whatever. 然后分析找到的值并采取任何措施。

So, using a little of what everyone told me, here is the answer: 因此,使用每个人告诉我的一些话,这就是答案:

@echo @ScriptToExecute.sql | sqlplus username/password@database | more 
+13 > result.txt
set /p "number=" < result.txt
set number=%number: =%
set /A newnumber=%number
echo %newnumber%:ok

So I first connect to the database and create result.txt with the answer in the first line. 因此,我首先连接到数据库,并在第一行中使用答案创建result.txt。 Then I put this first line in the variable number. 然后,将第一行放在变量号中。 I get rid of all the blank space before my number. 我摆脱了号码前的所有空白。 Finally I create a numeric value in which I put my string and I print it. 最后,我创建一个数值,在其中放置我的字符串并打印出来。

Thank you all for your answers :) 谢谢大家的答案 :)

Based on your new comments, this method should work: 根据您的新评论,此方法应起作用:

@echo off

echo @ScriptToExecute.sql | sqlplus username/password@database | more +13 > result.txt
for /F %%a in (result.txt) do set "number=%%a" & goto continue
:continue
echo %number%:ok

You may also try this way, that don't create any additional file: 您也可以这样尝试,即不创建任何其他文件:

@echo off

for /F "skip=13" %%a in ('echo @ScriptToExecute.sql ^| sqlplus username/password@database') do (
   set "number=%%a" & goto continue
)
:continue
echo %number%:ok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM