Sorting a 'Column' in a CSV or tab-delimited File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Sorting a 'Column' in a CSV or tab-delimited File

#16 Post by Squashman » 08 Sep 2014 09:59

Samir wrote:Thank you Squashman. Did you see the tokens=3* for my 3 column solution? This works, but it is correct?

You said it worked in your previous post so your hypotheses must be correct.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Sorting a 'Column' in a CSV or tab-delimited File

#17 Post by Samir » 08 Sep 2014 11:00

Squashman wrote:
Samir wrote:Thank you Squashman. Did you see the tokens=3* for my 3 column solution? This works, but it is correct?

You said it worked in your previous post so your hypotheses must be correct.
True, but I think 2* might have worked instead of the 1-2* you recommended for two columns (I never implemented 2 columns since I had a duplicate in column 2 and had to use a 3rd column).

When looking at the help for the for command, 1-2* and 2* do different things, so I'm wondering why 1-3* isn't what I needed to use vs 3* for my three column solution?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Sorting a 'Column' in a CSV or tab-delimited File

#18 Post by Squashman » 08 Sep 2014 11:59

Samir wrote:

Code: Select all

@echo off
(
  for /f "tokens=1-3* delims=," %%A in (test.csv) do @echo(%%C,%%B,%%A,%%B,%%C,%%D
)|sort|for /f "tokens=1* delims=," %%A in ('findstr "^"') do @echo(%%C


This was your original code where you were trying to sort by two columns. Because you are echoing %%C which in theory should have nothing in it because the tokens are set to 1*. That means %%A gets the value of %%C from the first echo and %%B gets everything else from the first echo. %%C would get nothing because it is the third token and you didn't assign anything to it. So technically your tokens should have been set to 1-2*.

Depending on how you set your tokens will factor into which tokens you are going to echo.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Sorting a 'Column' in a CSV or tab-delimited File

#19 Post by Samir » 08 Sep 2014 14:48

Squashman wrote:
Samir wrote:

Code: Select all

@echo off
(
  for /f "tokens=1-3* delims=," %%A in (test.csv) do @echo(%%C,%%B,%%A,%%B,%%C,%%D
)|sort|for /f "tokens=1* delims=," %%A in ('findstr "^"') do @echo(%%C


This was your original code where you were trying to sort by two columns. Because you are echoing %%C which in theory should have nothing in it because the tokens are set to 1*. That means %%A gets the value of %%C from the first echo and %%B gets everything else from the first echo. %%C would get nothing because it is the third token and you didn't assign anything to it. So technically your tokens should have been set to 1-2*.

Depending on how you set your tokens will factor into which tokens you are going to echo.
Okay, I understand this better. 8)

But then what would the difference between 1-2* and 2* be in the example that you just gave? Would they not both echo the same thing on %%a %%b and %%c?

Post Reply