2

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.

2
  • As far as I know, unless there is some sort of consistency such as a common letter separating all the samples (this includes a space) this is not possible. Note if each case is "A[number]" then it may be possible, however if there is no sort of consistency then it is not. Commented Jun 26, 2013 at 9:21
  • ^^sampleA1xxx ^^sampleA2yyyy ^^sampleA3zzzzz ^^sampleA4hhhhh if this is the case then there is ^^symbol always separating each entity...Is it possible now... Commented Jun 26, 2013 at 9:26

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

2
@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%!
      )
   )
)

Comments

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

Comments

0

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.

3 Comments

its not working for me . :(...Actually i need to find a keyword xxyyzzzz11.20.2 and get the version 11.20.2..
Did u replace "seperate.txt" with your file name?
hmm... I don't understand completely, but if you can find another consistency in the file, replace the =^^ with =[whatever separates each instance]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.