How add sort to %out% in "<%in% (code ...)> %out%

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How add sort to %out% in "<%in% (code ...)> %out%

#1 Post by alan_b » 04 Feb 2012 06:06

I can use code such as

Code: Select all

<%1 (
  for /L %%b in (!LO!,1,!HI!) do (
    set "LN=" & set /p "LN="
    ECHO(!LN!
  )
)> %2

After which I can sort %2, but I would like an elegant BUT VALID way of implementing

Code: Select all

<%1 (
  for /L %%b in (!LO!,1,!HI!) do (
    set "LN=" & set /p "LN="
    ECHO(!LN!
  )
) | SORT > %2


Regards
Alan

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How add sort to %out% in "<%in% (code ...)> %out%

#2 Post by Ed Dyreen » 04 Feb 2012 07:45

'
I'm having problems getting the output correctly, is it of help :(

Code: Select all

@echo off &setlocal enableDelayedExpansion

>test.TMP (
   ::
   echo.1It's too cold to think
   echo.2It's too cold to think
   echo.3It's too cold to think
   echo.4It's too cold to think
   echo.5It's too cold to think
)

set c=&for /f "usebackqdelims=" %%? in ("test.TMP") do set/ac+=1

<test.TMP (
   ::
   for /l %%? in (1,1,!c!) do (
      ::
      set ?=&set/p"?="

      if %%? geq 2 if %%? leq 4 (
         ::
         set $=&for /f %%? in ('^(for %%$ in ^(!?!^) do @echo^(%%$^)^|sort') do set "$=!$! %%?"
         echo(!$!
      )
   )
) >tested.TMP

type tested.TMP
pause

Code: Select all

 2It's cold think to too
 3It's cold think to too
 4It's cold think to too
Druk op een toets om door te gaan. . .

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How add sort to %out% in "<%in% (code ...)> %out%

#3 Post by dbenham » 04 Feb 2012 09:47

I'm not sure this is elegant, but I think this is the best you can do

Code: Select all

<%1 (
  cmd /q /v:on /c "for /L %%b in (!LO! 1 !HI!) do set LN=&set /p LN=&echo(!LN!"
) | sort >%2

For anyone who wants to test, I have this self contained variation. I assumed Alan has delayed expansion enabled, though it has no impact on the piped command.

Code: Select all

@echo off
setlocal enableDelayedExpansion
set LO=1
set HI=7
<%~f0 (
  cmd /q /v:on /c "for /L %%b in (!LO! 1 !HI!) do set LN=&set /p LN=&echo(!LN!"
) | sort

I ran into this problem before and asked on Stack Overflow: Why does delayed expansion fail when inside a piped block of code? The question opened up a whole can of worms. And it should be no surprise that jeb provided an excellent answer. The entire thread is worth reading.


Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How add sort to %out% in "<%in% (code ...)> %out%

#4 Post by Ed Dyreen » 04 Feb 2012 10:13

'
Ok, now I'm confused, are we trying to sort lines or are we trying inline sorting :?:
I always seem to misinterpret alan's posts somehow :o


My apologies :|

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How add sort to %out% in "<%in% (code ...)> %out%

#5 Post by dbenham » 05 Feb 2012 13:05

@Ed - Alan has a block of code within parentheses that relies on delayed expansion. It just so happens to be reading a text file and printing the first N lines. If he redirects the block's output to a file, and then sorts the file, everything works fine.

If he tries to pipe the block's output directly to the SORT command, the delayed expansion within the block fails. So the whole process fails.

The fact that alan is reading a file and using SORT is not important. The important issue is that any delayed expansion within a parenthesized block of code will fail if the block is on either side of a pipe operation. If you read the thread I cited then I think the problem and the solution may become clear.

@alan_b - If the file is large then it may be significantly more efficient to use the temporary file instead of the pipe solution. Besides that, the file solution is certainly easier to understand and maintain :!: :)

Dave Benham

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How add sort to %out% in "<%in% (code ...)> %out%

#6 Post by alan_b » 05 Feb 2012 15:13

@ Dave
Thank you for the link.

I thanked you last night also but my post is missing.
On another forum a couple of my posts have also gone missing.

I know the difference between a Preview and a Submit,
but I now suspect my browser and two websites have some sort of confusion.

I have tried to digest the can of worms you opened for me :lol:

I gave up and found a different and easier way.
My code filters out 60% of all (intermediate) input lines in #_CCLEANER.LST
and puts 40% in the final output file which I want sorted.
I actually start with a much larger file CCLEANER*.LOG and used FIND etc pipes to produce the intermediate #_CCLEANER.LST.
I now get the desired result after inserting SORT between two FIND's, thus

MORE CCLEANER*.LOG | FIND "*" | SORT /+47 | FIND /I "OnRuleStarted" > #_CCLEANER.LST || (
ECHO F A I L U R E - Probably due to U.A.C. blocking - Try "Right Click" then "Run As Administrator"
PAUSE >NUL
EXIT /B
)


Regards
Alan

Post Reply