简体   繁体   中英

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:

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

The answer is written into result.txt, it looks like this:

-- 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.

The answer I want is in the line number 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. 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. I tried using the command more +13 this way:

@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

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 ,

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. 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. User's responsibility to adjust for direct-from-prompt if desired.

Having set numreturned to nothing (ie not defined)

Read the file to %%a , selecting just the first token using spaces as a delimiter (default for tokens and delims)

When the first line of - appears (there are 10 - characters), set numreturned to some value so that it is defined.

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

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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