display all words starting with x?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

display all words starting with x?

#1 Post by taripo » 18 Aug 2014 09:23

suppose I have a file

Code: Select all

abc def xghi jkl xmno
xabc xdef ghi xjkl mno



I would like a batch file to run and for each line, display the words starting with x

so

it should display

Code: Select all

xghi xmno
xabc xdef xjkl


I don't know how many words there may be on a line

Not sure a good way to go about that.

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: display all words starting with x?

#2 Post by foxidrive » 18 Aug 2014 09:59

taripo, many times when someone answers a query and the data has been sanitised so it is nothing like the original
the answer doesn't work because of things like poison characters, unicode, foreign characters that were hidden from the task,
so that the people replying have to do the work twice.

Another aspect to this is that often the easiest code will use the characteristics of the text itself.

Just commenting because I can see the same problem occurring here.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: display all words starting with x?

#3 Post by taripo » 18 Aug 2014 12:22

there are no such characters, the question is as it is.

if anybody can answer it as it is then they have answered it.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: display all words starting with x?

#4 Post by Yury » 18 Aug 2014 12:48

Code: Select all

@echo off
for /f "usebackq delims=" %%i in ("example.txt") do (
 set word=%%i
 for /f "delims=" %%j in ('call echo "%%word: =" "%%"') do (
  for %%k in (%%j) do (
   if not "%%~k"=="" (
    set /a count_1+=1
    )
   )
  for %%k in (%%j) do (
   if not "%%~k"=="" (
    set /a count_2+=1
    for /f %%l in ('set /a count_1-count_2') do (
     if not %%l==0 (
      set /p=%%k<nul| findstr /i "^x">nul&& set /p"=%%~k "<nul
      ) else (
      set /p=%%k<nul| findstr /i "^x">nul&& set /p=%%k<nul
      )
     )
    )
   )
   echo.
  )
 )
pause>nul
exit /b

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: display all words starting with x?

#5 Post by Compo » 18 Aug 2014 13:44

A slightly different offering (change file.txt as appropriate):

Code: Select all

@Echo Off & SetLocal EnableExtensions EnableDelayedExpansion
For /F "Tokens=1* Delims=:" %%A In ('FindStr/in "\<x" file.txt') Do (
   For %%C In (%%B) Do (Echo(%%C|FindStr/ib x>Nul&&(If !Line_%%A!' Equ ' (
            Set Line_%%A=%%C) Else (Set Line_%%A=!Line_%%A! %%C))))
Set Line_
Pause & Exit/B
[EDIT]You may depending upon the number of matching lines wish to tweak the ordering for the line output.
I have just noticed your below response and decided that I may be better off doing that for you too![/EDIT]

Code: Select all

@Echo Off & SetLocal EnableExtensions EnableDelayedExpansion
For /F "Tokens=1* Delims=:" %%A In ('FindStr/in "\<x" file.txt') Do (
   For %%C In (%%B) Do (Echo(%%C|FindStr/ib x>Nul&&(If !Line_%%A!' Equ ' (
            Set Line_%%A=%%C) Else (Set Line_%%A=!Line_%%A! %%C)))
   Echo(%%Line_%%A%%=!Line_%%A!)
Pause & Exit/B
Last edited by Compo on 18 Aug 2014 14:33, edited 3 times in total.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: display all words starting with x?

#6 Post by taripo » 18 Aug 2014 13:49

Yury, Within this code.. (which is the kind of code you did in your code)

Code: Select all

@echo off
for /f "usebackq delims=" %%i in ("example.txt") do (
 set word=%%i
 for /f "delims=" %%j in ('call echo "%%word: =" "%%"') do (
   echo %%j
  )
)


What does 'call echo "%%word: =" "%%"' do?

One of the things that I don't understand there is %%var

I know for a for /f %f <-- in a batch file you do %%f I know that

but for a regular variable, the double percentage, I haven't see that before

And having a call statement.. i've seen that for a batch file.. call a batch file. And i've see that for a label. But call echo .. ?

I'm familiar with the syntax shown in set /?, of ECHO %var:a=b% Though that never uses double percentage as far as I can tell.. What's the %%word and the "%%"?

Could you explain what that line is doing?

Thanks

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: display all words starting with x?

#7 Post by Aacini » 18 Aug 2014 17:21

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in ('findstr /I "\<x" file.txt') do (
   set "line="
   for %%b in (%%a) do (
      set "word=%%b"
      if /I "!word:~0,1!" equ "x" set "line=!line! %%b"
   )
   echo !line:~1!
)


Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: display all words starting with x?

#8 Post by foxidrive » 19 Aug 2014 06:31

taripo wrote:there are no such characters, the question is as it is.


Nobody knew that until your reply to me - as you provided rubbish as an example.

Anyway, you have some replies now...

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: display all words starting with x?

#9 Post by taripo » 19 Aug 2014 07:08

foxidrive wrote:
taripo wrote:there are no such characters, the question is as it is.


Nobody knew that until your reply to me - as you provided rubbish as an example.

Anyway, you have some replies now...


I didn't say anybody knew or didn't know. I did not provide rubbish, I provided an example and that enabled people to give answers. And yeah I can see the answers given. I don't know why you think I cannot see replies or why you think that I can only see your replies. You asked me a question and I answered you. I don't know what you want from me.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: display all words starting with x?

#10 Post by Yury » 19 Aug 2014 07:15

Aacini wrote:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in ('findstr /I "\<x" file.txt') do (
   set "line="
   for %%b in (%%a) do (
      set "word=%%b"
      if /I "!word:~0,1!" equ "x" set "line=!line! %%b"
   )
   echo !line:~1!
)


Antonio



Antonio, what your code says about this text:

Code: Select all

a!bc d&ef x=ghi j|kl x;mno
x<abc x,def g>hi xjkl mno


???

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: display all words starting with x?

#11 Post by Aacini » 19 Aug 2014 08:34

Yury wrote:Antonio, what your code says about this text:

Code: Select all

a!bc d&ef x=ghi j|kl x;mno
x<abc x,def g>hi xjkl mno



@Yury:

foxidrive wrote:taripo, many times when someone answers a query and the data has been sanitised so it is nothing like the original the answer doesn't work because of things like poison characters, unicode, foreign characters that were hidden from the task, so that the people replying have to do the work twice.


taripo wrote:there are no such characters, the question is as it is.

if anybody can answer it as it is then they have answered it.

So???


Antonio

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: display all words starting with x?

#12 Post by Yury » 19 Aug 2014 08:45

Aacini wrote:So???


Antonio, so!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: display all words starting with x?

#13 Post by foxidrive » 20 Aug 2014 08:56

taripo wrote:I did not provide rubbish, I provided an example
I don't know what you want from me.


This might explain.

viewtopic.php?p=36341#p36341

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: display all words starting with x?

#14 Post by taripo » 20 Aug 2014 10:13

foxidrive wrote:
taripo wrote:I did not provide rubbish, I provided an example
I don't know what you want from me.


This might explain.

viewtopic.php?p=36341#p36341


I understand and understood what you meant. And you thought it was not easy enough for you to answer because you thought I was asking something else / that the answer to my question would not be what I wanted. But I was clear and other people had no problem.

I'm familiar with seeing people that ask one question and mean another.. Personally.. my approach would still be to answer what they ask regardless, and if they meant to ask something else they can ask something else. However, I was asking what I asked.

I was not keen on writing many paragraphs trying to prove to you (some kind of misguided skeptic) that I am actually asking what I am asking. or to try to explain the motivations for my question so that you could be convinced that I am indeed asking what i'm asking. I understand your point of view, but it has the weakness that if somebody is actually asking what they ask, and it doesn't look like the specific case you're thinking they want, then there may be no easy way for them to convince you. If you were to say "most often this/such a question, is not what people are asking, but sometimes people really are, is this the case here?" then I could say yes it is. Done. Easy. But I don't know exactly what it'd take to convince you or to let you accept it, because you didn't even lay out that option. Nor do I know if you're philosophically inclined enough to accept(or even listen to) an argument that counters what looks like the only possibility you have considered. And if something could convince you or lead you to accept it, then I don't know how many words and posts it'd require to do so - so in that sense, I don't know what you want from me. It could get tedious and I don't think you have any interest in a tedious discussion(that has little to do with batch files) either, in fact were we to go in that direction you might start saying I am a troll. So you're leading me to where if I were to explain it to you I don't know what it'd take or if it's even possible, and it looks like you'd make it difficult too - at best.

Suffice it to say, -fortunately- people were able to understand, respected the question enough to accept it, and were willing to answer my question - the question I asked!

There is no need for you to tell me they have answered it(as you did), I can see they have, and I never said or suggested that they hadn't.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: display all words starting with x?

#15 Post by foxidrive » 20 Aug 2014 10:23

taripo wrote:Suffice it to say, -fortunately- people were able to understand, respected the question enough to accept it, and were willing to answer my question - the question I asked!


You do realise that they all answered after they knew what the text contained, right?

Your example was so obviously bogus and your question didn't contain enough information for anyone to know the makeup of the text.

As for answering the question as posed - sure, but answer the question as posed and then find out you have wasted your time when the OP replies to say it doesn't work, and you have to ask 20 questions to find out why the code failed.

It gets really wearing after the first 5000 times or so.

taripo, do you actually give up your free time to help random people every day of the week, free of charge?
Do it for several years and you might just change your attitude.

Post Reply