Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to extract nth word from a list  (Read 7616 times)

0 Members and 1 Guest are viewing this topic.

Frank

    Topic Starter


    Intermediate

    Thanked: 3
    How to extract nth word from a list
    « on: February 22, 2021, 09:32:58 PM »
    I would like to extract the nthe word from a list.
    As an example, if I have a list "one two three four" and would like to extract word 3, how would I do this in a batch?

    Hackoo



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    Re: How to extract nth word from a list
    « Reply #1 on: February 23, 2021, 12:27:36 AM »
    Hi  ;)
    Please be more explicit ! ;)
    What's the delimiters of your list and where this is located on text file or from another command ??
    The word is in a phrase with one line or with newline ?

    The word is in a phrase with one line
    Code: [Select]
    one two three four
    Or with newline
    Code: [Select]
    one
    two
    three
    four

    Can you provide us a real example and what code did you tried until now ?

    Here is an example :
    Code: [Select]
    @echo off
    Title Extarct word from list
    set "Line=one two three four five"
    set "Num=3"
    SetLocal EnableDelayedExpansion
    @for /f "tokens=%Num%" %%i in ("%Line%") do set "word_%Num%=%%i"
    echo The extracted word is = !word_%Num%!
    pause
    « Last Edit: February 23, 2021, 12:45:01 AM by Hackoo »

    Frank

      Topic Starter


      Intermediate

      Thanked: 3
      Re: How to extract nth word from a list
      « Reply #2 on: February 23, 2021, 01:22:36 AM »
      I think I over simplified my question.
      Here is the script I have been working on with your addition now:

      SETLOCAL EnableDelayedExpansion
      set attribs=--a------
      echo Attributes: %attribs%
      FOR /L %%G IN (0,1,8) DO (
         set val=!attribs:~%%G,1!
         set num=%%G
         if not !val!==- (
         echo Attribute: Got here with !num!
         set line="Directory Read_Only Archived Hidden System_File Compressed_File Offline_File Temporary_File Reparse_point"
         @for /f "tokens=%Num%" %%i in ("%Line%") do set "word_%Num%=%%i"
         echo The extracted word is = !word_%Num%!
              )
      )

      The "Got here with !num!" is correct as "Got here with 2".
      Then the !word_%Num%! is a blank.
      This of course is inside a loop and needs expansion but I am lost here since I had to use !num! first but if I use !num! with the token I receive an error.
      How do I work around this?

      Hackoo



        Hopeful
      • Thanked: 42
      • Experience: Expert
      • OS: Windows 10
      Re: How to extract nth word from a list
      « Reply #3 on: February 23, 2021, 03:51:59 AM »
      Just give a try for this modification
      NB : I choose ; as delimters

      Code: [Select]
      @echo off
      Title Extarct word from list
      SetLocal EnableDelayedExpansion
      set "attribs=--a------"
      set "line=Directory;Read_Only;Archived;Hidden;System_File;Compressed_File;Offline_File;Temporary_File;Reparse_point"
      echo Attributes: %attribs%

      @for /L %%G IN (0,1,8) DO (
      set val=!attribs:~%%G,1!
      if not "!val!"=="-" (
      set "num=%%G"
      )
      )

      echo Got here with %num%

      @for /f "tokens=%num% delims=;" %%i in ("%Line%") do (
      set "word=%%i"
      echo The extracted word is = !word!
      )
      Pause &  EXIT

      Frank

        Topic Starter


        Intermediate

        Thanked: 3
        Re: How to extract nth word from a list
        « Reply #4 on: February 23, 2021, 03:31:39 PM »
        Thank you Hackoo.

        Your code works but needed to add:
        set /A num+1
        after set "num=%%G" to extract the correct word in the second for loop.

        There is a problem though. I originally placed the 2nd loop inside the first to be able to display multiple words for files that exhibit multiple attributes (eg. Read only and System file [-r--s----]).
        This code only displays the last attribute.

        Frank

          Topic Starter


          Intermediate

          Thanked: 3
          Re: How to extract nth word from a list
          « Reply #5 on: February 23, 2021, 04:20:53 PM »
          I think I have it worked out now.
          Rather than use a second loop after the first, call the second loop as a subroutine from inside the first loop as follows:

          @echo off
          Title Extarct word from list
          SetLocal EnableDelayedExpansion
          set "attribs=-r--s----"
          set "line=Directory;Read Only;Archived;Hidden;System File;Compressed File;Offline File;Temporary File;Reparse point"
          echo Attributes: %attribs%

          @for /L %%G IN (0,1,8) DO (
             set val=!attribs:~%%G,1!
             if not "!val!"=="-" (
                set "num=%%G"
                set /A num+=1
                call :reit
             )
          )
          pause
          exit

          :reit
          @for /f "tokens=%num% delims=;" %%i in ("%Line%") do (
             set "word=%%i"
             echo The extracted word is = !word!
          )
          goto :eof
          Pause &  EXIT

          Just on the side, how do you insert code into the post as you have done Hackoo?
           

          Hackoo



            Hopeful
          • Thanked: 42
          • Experience: Expert
          • OS: Windows 10
          Re: How to extract nth word from a list
          « Reply #6 on: February 23, 2021, 11:56:18 PM »
          Hi Frank  ;)
          I Just have one question :
          How did you obtain this format, i mean which command exactly can you get this ?
          Code: [Select]
          -r--s----
          Just on the side, how do you insert code into the post as you have done Hackoo?
          I'm glad that you have solved your issue !
          Did you mean how to format code into [ code ][ /code ] ?
          Just put your entire code between [ code ][ /code ] (Just trim the extra spaces between brackets) or select all your code and hit the button # for code !
          And if you want to thanks someone , you have just a little link Thank member name  ;) :) :P

          Frank

            Topic Starter


            Intermediate

            Thanked: 3
            Re: How to extract nth word from a list
            « Reply #7 on: February 24, 2021, 01:39:10 PM »
            Hi Hackoo,
            The format for the file attributes (-r--s----) I obtained while searching the internet here:

            https://stackoverflow.com/questions/9252980/how-to-split-the-filename-from-a-full-path-in-batch

            See the code extract:
            Code: [Select]
            set "filename=C:\Folder1\Folder2\File.ext"
            For %%A in ("%filename%") do (
                echo full path: %%~fA
                echo drive: %%~dA
                echo path: %%~pA
                echo file name only: %%~nA
                echo extension only: %%~xA
                echo expanded path with short names: %%~sA
                echo attributes: %%~aA
                echo date and time: %%~tA
                echo size: %%~zA
                echo drive + path: %%~dpA
                echo name.ext: %%~nxA
                echo full path + short name: %%~fsA)
            Note the line 'echo attributes: %%~aA
            I have now noted the link 'Thank member'.
            Thank you for your patience.