locked
How to use delims that start or end with a space (or other special characters) when executing the command “for”? RRS feed

  • Question

  • How to use delims that start or end with a space (or other special characters) when executing the command "for"?

    1. Execute: [for / f "delims = tokens = *"% a in (a.txt) do @echo% a]
      Result:   [ a bb ccc ddd 1234]
    2. Execute: [for / f "tokens = *"% a in (a.txt) do @echo% a]
      Result:   [a bb ccc ddd 1234]
    3. Execute: [for / f "delims = '' tokens = *"% a in (a.txt) do @echo% a]
      Result:   [Error: There should be no 'tokens = * "at this time]

    Note: The above is just an example.

    A space is added to the first line of the first execution result;
    The second execution result is the expected result;
    The third execution prompts a syntax error.

    Thursday, March 19, 2020 10:46 AM

Answers

  • Ok, but I still don't understand your method. Anyway, thank you very much.

    Powershell is very powerful and there are shortcuts that you can use to reduce the amount of code and speed up processing.

    Here is a verbose example that does the same thing. 

    # comments begin with a # character
    # this is a powershell script
    cls                                  # clear the screen
    $lines = Get-Content file.txt        # read the lines in the file.txt onto the $lines variable 
    "Here are the lines that we found"   # write out a comment
    $lines                               # show what we read
    ""                                   # a blank line 
    
     ForEach ($line in $lines) {         # process each line one by one in the $lines collection 
            $wordswithspaces = $line -split ','       # now split the data
            "Here is the array that we split"         # a comment
            $wordswithspaces                          # the data
            ""                                        # a blank line
            "Here are the words with the spaces trimmed off" 
            foreach ($word in $wordswithspaces) {       # process each word that we split
                $word.Trim()                            # trim off the spaces. This is the variable that you would use for further procesing
            } 
    }

    Saturday, March 21, 2020 1:46 PM

All replies

  • You should not be using batch files.  Use PowerShell.

    What is it you are trying to do?  Don't tell us what code you want to use just describe the purpose for this.

    Are you trying to split strings on a space?


    \_(ツ)_/

    Thursday, March 19, 2020 11:13 AM
  • Here, the string is split using spaces. But it is also possible to split strings using "," and other strings that start or end with spaces. But I can't make them as a whole delimiter in the command.
    Thursday, March 19, 2020 2:50 PM
  • Please read jrv's response again.

    Also read this:

    http://xyproblem.info/


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 19, 2020 5:35 PM
  • Here, the string is split using spaces. But it is also possible to split strings using "," and other strings that start or end with spaces. But I can't make them as a whole delimiter in the command.

    You have to provide a clear example of what you are trying to split.  Your question is still too ambiguous to answer.


    \_(ツ)_/

    Thursday, March 19, 2020 9:37 PM
  • Ok, here is a simple example.

    In the "a.txt" file, there is only one line of data, like "a , bb , ccc , 1234". I need to get "a", "bb", "ccc", "1234" by splitting. If I use "," as the delimiter, then the result will be extra spaces. If I use " , " as the delimiter, it will prompt a syntax error.

    How to achieve it with the command for?

    Friday, March 20, 2020 11:13 AM
  • Now that makes some sense and has a really easy solution.

    'a , bb , ccc , 1234' -split ',' |%{$_.Trim()}

    See how much easier it is to be understood when you don't try to say how you want to do something and just say what you want to do and give a full example.


    \_(ツ)_/


    • Edited by jrv Friday, March 20, 2020 11:43 AM
    Friday, March 20, 2020 11:43 AM
  • Sorry, I could not understand your answer.
    In fact, I need to use the command(for) to traverse each line in the file a.txt and split it, and then pass a certain value obtained by the split to perform other operations. I found a method on https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc754900(v=ws.10)

    But I cannot use " , " as delims. Here is an example at the bottom of this website: 

    for /f "eol=; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %k


    Saturday, March 21, 2020 11:31 AM
  • Please refrain from using batch as it is obsolete.

    Get-Content file.txt | 
        ForEach-Object{
            $_ -split ',' | %{$_.Trim()} 
        }


    \_(ツ)_/

    Saturday, March 21, 2020 11:35 AM
  • Ok, but I still don't understand your method. Anyway, thank you very much.
    Saturday, March 21, 2020 12:01 PM
  • Ok, but I still don't understand your method. Anyway, thank you very much.

    Powershell is very powerful and there are shortcuts that you can use to reduce the amount of code and speed up processing.

    Here is a verbose example that does the same thing. 

    # comments begin with a # character
    # this is a powershell script
    cls                                  # clear the screen
    $lines = Get-Content file.txt        # read the lines in the file.txt onto the $lines variable 
    "Here are the lines that we found"   # write out a comment
    $lines                               # show what we read
    ""                                   # a blank line 
    
     ForEach ($line in $lines) {         # process each line one by one in the $lines collection 
            $wordswithspaces = $line -split ','       # now split the data
            "Here is the array that we split"         # a comment
            $wordswithspaces                          # the data
            ""                                        # a blank line
            "Here are the words with the spaces trimmed off" 
            foreach ($word in $wordswithspaces) {       # process each word that we split
                $word.Trim()                            # trim off the spaces. This is the variable that you would use for further procesing
            } 
    }

    Saturday, March 21, 2020 1:46 PM