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 4581 times)

0 Members and 1 Guest are viewing this topic.

sponske

    Topic Starter


    Starter

    • 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


    • Intermediate
    • Thanked: 15
      • 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%
      Do not communicate by sharing memory; instead, share memory by communicating.

      --Effective Go

      Salmon Trout

      • Guest
      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

      • Guest
      Re: delete lines in text
      « Reply #3 on: January 12, 2019, 05:05:40 AM »
      You don't need setlocal enabledelayedexpansion.

      Blisk



        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: delete lines in text
        « Reply #4 on: February 02, 2019, 02:53:57 AM »
        I was looking for similair script, which delete only one word for example "computer" from txt file and that word is in random places in txt file.
        Didn't find anything usefull.

        Salmon Trout

        • Guest
        Re: delete lines in text
        « Reply #5 on: February 02, 2019, 05:49:20 AM »
        Do you mean if a line in a text file says

        bread meat computer 1234

        You want the line to be written out as

        bread meat 1234    .... ?

        Or you want that whole line to be skipped?

        Or is the format different? (spaces, whatever?)

        It would be good if you give examples.



        nil

        • Global Moderator


        • Intermediate
        • Thanked: 15
          • Experience: Experienced
          • OS: Linux variant
          Re: delete lines in text
          « Reply #6 on: February 02, 2019, 06:17:53 AM »
          I was looking for similair script, which delete only one word for example "computer" from txt file and that word is in random places in txt file.
          Didn't find anything usefull.

          On anything but a Windows computer you would use sed -

          https://www.computerhope.com/unix/used.htm

          In sed, this would be

          Code: [Select]
          sed -i 's/computer//' myfile.txt
          Sed is available for Windows if you enable WSL:

          https://www.computerhope.com/issues/ch001879.htm

          Or, if you are comfortable with Powershell, you can use get-content:

          https://stackoverflow.com/a/6028937

          Hope this helps -
          Do not communicate by sharing memory; instead, share memory by communicating.

          --Effective Go

          sponske

            Topic Starter


            Starter

            • Experience: Beginner
            • OS: Windows 10
            Re: delete lines in text
            « Reply #7 on: February 02, 2019, 06:21:29 AM »
            Thx, i'm currently on holiday i'll get into this asap when I return home!

            Salmon Trout

            • Guest
            Re: delete lines in text
            « Reply #8 on: February 02, 2019, 07:28:28 AM »
            On anything but a Windows computer you would use sed -

            https://www.computerhope.com/unix/used.htm

            In sed, this would be

            Code: [Select]
            sed -i 's/computer//' myfile.txt
            Sed is available for Windows if you enable WSL:

            or get a GNU build for Windows

            https://kent.dl.sourceforge.net/project/gnuwin32/sed/4.2.1/sed-4.2.1-setup.exe

            Blisk



              Intermediate

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Re: delete lines in text
              « Reply #9 on: February 07, 2019, 12:37:57 AM »
              Do you mean if a line in a text file says

              bread meat computer 1234

              You want the line to be written out as

              bread meat 1234    .... ?

              Or you want that whole line to be skipped?

              Or is the format different? (spaces, whatever?)

              It would be good if you give examples.

              yes exactly that what you give an example.
              Like this
              bread meat  1234
              meat computer bread  1234
              bread 1234 meat computer

              result will be
              bread meat  1234
              meat  bread  1234
              bread 1234 meat 

              sponske

                Topic Starter


                Starter

                • Experience: Beginner
                • OS: Windows 10
                Re: delete lines in text
                « Reply #10 on: February 11, 2019, 01:53:29 PM »
                my target is to output the info into a CSV file

                with the info from above i can skip a lot of tmp files creating thx for starting :)

                but issue remains the same, for example:

                one of the results is:     Receive rate (Mbps)    :  360
                I would like my script to
                   ==>  remove the Receive rate (Mbps)     :
                   ==>  keep the 360

                I this way i can create headers in the CSV file with "Receive rate", "signal", "connection mode",...
                And the info will be below

                The script wil run every x minutes and add the info to a csv file.

                Quote
                    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

                can become

                Quote
                    WiFi
                    Intel(R) Wireless WiFi Link 4965AGN
                    0232ddc7-6cc3-4110-ba5d-3b6579a51327
                    00:21:5c:5a:90:bb
                    connected
                    VM813447-5G
                    50:6a:03:6d:f9:30
                    Infrastructure
                    802.11g
                    WPA2-Personal
                    CCMP
                    Auto Connect
                    44
                    45
                    45
                    67%
                    VM813447-5G

                Salmon Trout

                • Guest
                Re: delete lines in text
                « Reply #11 on: February 11, 2019, 02:22:43 PM »
                You can use FOR /F to split the line on the colon (:) symbol.

                Some command output with colons:

                C:\>Netsh WLAN show interfaces

                There is 1 interface on the system:

                    Name                   : Wi-Fi
                    Description            : Intel(R) Wireless WiFi Link 4965AGN
                    GUID                   : 53b568e2-3211-4942-942f-90de2bf30a9f
                    Physical address       : 00:21:5c:5a:90:bb
                    State                  : disconnected
                    Radio status           : Hardware On
                                             Software On

                    Hosted network status  : Not available


                in a batch script:

                @echo off
                for /f "tokens=1* delims=:" %%A in ('netsh wlan show interfaces') do echo.%%B



                 Wi-Fi
                 Intel(R) Wireless WiFi Link 4965AGN
                 53b568e2-3211-4942-942f-90de2bf30a9f
                 00:21:5c:5a:90:bb
                 disconnected
                 Hardware On

                 Not available