rvp
rvp

Reputation: 580

to find a particular string alone using windows batch script

if my file conatins below text :

sampleA1xxx sampleA2yyyy sampleA3zzzzz ... sampleA4hhhhh

I want to find sampleA4 and display sampleA4hhhh using windows batch script.

Thats is my output should be: sampleA4hhhhh

Could anyone please help me.

Upvotes: 2

Views: 1298

Answers (4)

Magoo
Magoo

Reputation: 80203

@ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (q17316008.txt) DO SET line=%%i
SET line=%line:^= %
SET line=%line:*sampleA4=sampleA4%
FOR %%i IN (%line:^^= %) DO SET line=%%i&GOTO :done
:done
ECHO %line%

GOTO :EOF

This should do what I gather to be the task. It assumes that the "samplea4" string exists in the file's single line, is case-insensitive and the line doesn't exceed the ~8K limit on line length.

Simply replace the carets with spaces, lop off the leading characters to the first occurrence of the target string, and process that target string as a list; the first element will be the required string, so stop the processing when it's available.

Upvotes: 2

Aacini
Aacini

Reputation: 67256

@echo off
setlocal EnableDelayedExpansion
set target=sampleA4
set len=8
for /F "delims=" %%a in ('findstr "%target%" theFile.txt') do (
   for %%b in (%%a) do (
      set word=%%b
      if "!word:~0,%len%!" equ "%target%" (
         echo !word:~%len%!
      )
   )
)

Upvotes: 2

Endoro
Endoro

Reputation: 37589

take a batch or have a look at GNUWin sed:

>type file
^^sampleA1xxx ^^sampleA2yyyy ^^sampleA3zzzzz ^^sampleA4hhhhh

>sed -r "s/.*(\b\w+4\w+)/\1/" file
sampleA4hhhhh

Upvotes: 2

Monacraft
Monacraft

Reputation: 6620

OK, I tried this myself with a similar case and it worked fine:

for /f "tokens=4 delims=^^" %%a in (seperate.txt) do (echo %%a)
pause

Note: This is designed for a batch file, and replace the 4 after "tokens=" with whichever separated text you want to stop at.

Hope this helped,

Yours Mona.

Upvotes: 0

Related Questions