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

Author Topic: delete lines in text  (Read 291 times)

0 Members and 2 Guests are viewing this topic.

sponske

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 10
    delete lines in text
    « on: January 10, 2019, 04:13:19 PM »
    Hi there,

    I'm currently creating a batch in windows (10) to export info to a csv file.
    The command i need to expert is:

    NETSH WLAN SHOW INTERFACE

    I write this info to a TXT file

    I get an output like this:

    Quote
        Name                   : Wi-Fi
        Description            : Intel(R) Centrino(R) Ultimate-N 6300 AGN
        GUID                   : 91a6ebce-01cf-4d36-87ab-1e630893718a
        Physical address    : 3c:a9:f4:09:fb:9c
        State                  : connected
        SSID                   : WiFi_2.4_MP
        BSSID                  : e2:b9:e5:e1:5c:7e
        Network type           : Infrastructure
        Radio type             : 802.11n
        Authentication         : WPA2-Personal
        Cipher                 : CCMP
        Connection mode        : Profile
        Channel                : 44
        Receive rate (Mbps)    : 150
        Transmit rate (Mbps)   : 150
        Signal                 : 72%
        Profile                : WiFi_2.4_MP

    I only want the info behind the ":" , the results...
    So i'm using this script to delete them:

    Quote
    set "a1=    Name                   : "
    set "b1=    Description            : "
    set "c1=    GUID                   : "
    set "d1=    Physical address       : "

    FOR /f "delims=" %%a IN (config.log) DO (
        set b=%%a
        echo !b:%a1%=!>>configa1.tmp
    )

    FOR /f "delims=" %%a IN (configa1.tmp) DO (
        set b=%%a
        echo !b:%b1%=!>>configb1.tmp
    )

    FOR /f "delims=" %%a IN (configb1.tmp) DO (
        set b=%%a
        echo !b:%c1%=!>>configc1.tmp
    )

    FOR /f "delims=" %%a IN (configc1.tmp) DO (
        set b=%%a
        echo !b:%d1%=!>>configd1.tmp

    This all works well until the script gets to the following line:

    Quote
    set "n1=    Receive rate (Mbps)    : "
    set "o1=    Receive rate (Mbps)    : "
    FOR /f "delims=" %%a IN (configm1.tmp) DO (
        set b=%%a
        echo !b:%n1%=!>>confign1.tmp
    )

    FOR /f "delims=" %%a IN (confign1.tmp) DO (
        set b=%%a
        echo !b:%o1%=!>>configo1.tmp
    )

    The reason is the text (Mbps), i manage to remove (Mbps, but from the moment i add ")" the script crashes.
    I can't find out how to fix it.

    Anyone with a golden tip to get me true the last part?

    Greetings

    nil

    • Global Moderator


    • Beginner

      Thanked: 4
      • Experience: Experienced
      • OS: Linux variant
      Re: delete lines in text
      « Reply #1 on: January 11, 2019, 05:20:52 PM »
      try this:
      Code: [Select]
      set "str=Name                   : Wi-Fi"
      set "result=%str:: =" & set "result=%"
      echo %result%

      Salmon Trout



        Genius

        Thanked: 935
        • Yes
      • Computer: Specs
      • Experience: Experienced
      • OS: Other
      Re: delete lines in text
      « Reply #2 on: January 12, 2019, 04:01:39 AM »
      You can do it in one pass, without any temp files.

      Use setlocal enabledelayedexpansion so that you can expand and SET %variables% in the loop.
      Use FOR /F to parse the output of NETSH WLAN SHOW INTERFACE directly.
      Use a "tokens=1* delims=:" block in the FOR /F command to split each output line into two tokens; the delimiter is a colon.
      Echo each first token into a pipe through FIND, redirecting to NUL to hide it from the console (avoids screen clutter).
      If FIND returns a zero errorlevel the && operator runs the subsequent SET command.
      %%A is the first part (pre-colon). The implicit %%B contains everything after the first delimiter in each line, because...
      ... we use an asterisk like this tokens=1* so that only the first delim splits; everything else to the line end is put in %%B...
      ... if you don't do that, the line gets split again by the first colon in the MAC address, and you only capture the first byte.
      Protect the loop operation from being broken by ( and ) characters by using quotes around SET assignations in the loop.
      Pipe through FIND twice for 'SSID' because otherwise FIND will discover those characters within 'BSSID' (FIND /V excludes).

      A good editor with column insert/select/copy/paste makes this kind of thing much easier. I use SCITE. Also Geany has that feature.

      My results:

      (1) Run NETSH WLAN SHOW INTERFACE

      c:\Batch\Test>NETSH WLAN SHOW INTERFACE

      There is 1 interface on the system:

          Name                   : WiFi
          Description            : Intel(R) Wireless WiFi Link 4965AGN
          GUID                   : 0232ddc7-6cc3-4110-ba5d-3b6579a51327
          Physical address       : 00:21:5c:5a:90:bb
          State                  : connected
          SSID                   : VM813447-5G
          BSSID                  : 50:6a:03:6d:f9:30
          Network type           : Infrastructure
          Radio type             : 802.11g
          Authentication         : WPA2-Personal
          Cipher                 : CCMP
          Connection mode        : Auto Connect
          Channel                : 44
          Receive rate (Mbps)    : 45
          Transmit rate (Mbps)   : 45
          Signal                 : 67%
          Profile                : VM813447-5G

          Hosted network status  : Not available


      (2) This is the batch

      @echo off

      for /f "tokens=1* delims=:" %%A in ('NETSH WLAN SHOW INTERFACE') do (
         echo %%A | find "Name"                   > nul && set "a1=    Name                   : %%B"
         echo %%A | find "Description"            > nul && set "b1=    Description            : %%B"
         echo %%A | find "GUID"                   > nul && set "c1=    GUID                   : %%B"
         echo %%A | find "Physical address"       > nul && set "d1=    Physical address       : %%B"
         echo %%A | find "State"                  > nul && set "e1=    State                  : %%B"
         echo %%A | find "SSID" | find /v "BSSID" > nul && set "f1=    SSID                   : %%B"
         echo %%A | find "BSSID"                  > nul && set "g1=    BSSID                  : %%B"
         echo %%A | find "Network type"           > nul && set "h1=    Network type           : %%B"
         echo %%A | find "Radio type"             > nul && set "i1=    Radio type             : %%B"
         echo %%A | find "Authentication"         > nul && set "j1=    Authentication         : %%B"
         echo %%A | find "Cipher"                 > nul && set "k1=    Cipher                 : %%B"
         echo %%A | find "Connection mode"        > nul && set "l1=    Connection mode        : %%B"
         echo %%A | find "Channel"                > nul && set "m1=    Channel                : %%B"
         echo %%A | find "Receive rate (Mbps)"    > nul && set "n1=    Receive rate (Mbps)    : %%B"
         echo %%A | find "Transmit rate (Mbps)"   > nul && set "o1=    Transmit rate (Mbps)   : %%B"
         echo %%A | find "Signal"                 > nul && set "p1=    Signal                 : %%B"
         echo %%A | find "Profile"                > nul && set "q1=    Profile                : %%B"
         )

      echo value of a1 is: %a1%
      echo value of b1 is: %b1%
      echo value of c1 is: %c1%
      echo value of d1 is: %d1%
      echo value of e1 is: %e1%
      echo value of f1 is: %f1%
      echo value of g1 is: %g1%
      echo value of h1 is: %h1%
      echo value of i1 is: %i1%
      echo value of j1 is: %j1%
      echo value of k1 is: %k1%
      echo value of l1 is: %l1%
      echo value of m1 is: %m1%
      echo value of n1 is: %n1%
      echo value of o1 is: %o1%
      echo value of p1 is: %p1%
      echo value of q1 is: %q1%


      (3) This is the output of the batch:

      value of a1 is:     Name                   :  WiFi
      value of b1 is:     Description            :  Intel(R) Wireless WiFi Link 4965AGN
      value of c1 is:     GUID                   :  0232ddc7-6cc3-4110-ba5d-3b6579a51327
      value of d1 is:     Physical address       :  00:21:5c:5a:90:bb
      value of e1 is:     State                  :  connected
      value of f1 is:     SSID                   :  VM813447-5G
      value of g1 is:     BSSID                  :  50:6a:03:6d:f9:30
      value of h1 is:     Network type           :  Infrastructure
      value of i1 is:     Radio type             :  802.11g
      value of j1 is:     Authentication         :  WPA2-Personal
      value of k1 is:     Cipher                 :  CCMP
      value of l1 is:     Connection mode        :  Auto Connect
      value of m1 is:     Channel                :  44
      value of n1 is:     Receive rate (Mbps)    :  60
      value of o1 is:     Transmit rate (Mbps)   :  60
      value of p1 is:     Signal                 :  65%
      value of q1 is:     Profile                :  VM813447-5G


      « Last Edit: January 12, 2019, 05:02:46 AM by Salmon Trout »

      Salmon Trout



        Genius

        Thanked: 935
        • Yes
      • Computer: Specs
      • Experience: Experienced
      • OS: Other
      Re: delete lines in text
      « Reply #3 on: January 12, 2019, 05:05:40 AM »
      You don't need setlocal enabledelayedexpansion.