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

Author Topic: dos for command  (Read 3862 times)

0 Members and 1 Guest are viewing this topic.

gkata

    Topic Starter


    Newbie

    dos for command
    « on: June 11, 2009, 06:35:55 AM »
    I have a file log file for which I need to read each line.  If a line in the log file starts with the word 'Record' or 'ORA' I want to capture the complete line and write it to a new file.

    I have this, but it doesn't work correctly:

    for /f %%a in (main.log) do (
    echo %%a >> newfile.txt
    )

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: dos for command
      « Reply #1 on: June 11, 2009, 08:41:58 AM »
      use findstr . type findstr /? to find out more

      gkata

        Topic Starter


        Newbie

        Re: dos for command
        « Reply #2 on: June 11, 2009, 08:49:43 AM »
        But how do incorporate the findstr command into the 'for' loop?

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: dos for command
          « Reply #3 on: June 11, 2009, 09:44:25 AM »
          sorry dude, i don't do batch.
          you can search for similar solutions in the forum (there are plenty) , or you can wait for the batch enthusiast to give you a hand.


          Woodman



            Beginner
            Re: dos for command
            « Reply #4 on: June 12, 2009, 05:34:28 AM »
            This might work for you:

            Code: [Select]
            @echo off > newfile.txt
            cls
            setlocal enabledelayedexpansion

            for /f "delims=*" %%A in (main.log) do (
                set line=%%A & call :findit
            )
            exit /b

            :findit
            for /f %%B in ("!line!") do (
                if %%B equ Record echo !line!>>newfile.txt
                if %%B equ ORA echo !line!>>newfile.txt
            )

            Good luck.


            billrich

            • Guest
            Re: dos for command
            « Reply #5 on: June 12, 2009, 06:42:34 AM »
            gkata,

            ( The following code works. I have tested and retested the code. )

            C:\>type mainlog.txt
            Record  bbbbbbbbbbbbbbbbbb
            ORA  xxxxxxxxxxxxxxxxxxxxxxxx
            Now is the time to find

            C:\>type orastr.bat
            Code: [Select]
            @echo off

            setlocal enabledelayedexpansion
            echo Hello > newfile.txt

            for /f "delims=*" %%A in (mainlog.txt) do (
            echo %%A  |  findstr  "ORA Record" >> newfile.txt

            )
            type newfile.txt

            Output:

            C:\>type newfile.txt
            Hello
            Record  bbbbbbbbbbbbbbbbbb
            ORA  xxxxxxxxxxxxxxxxxxxxxxxx

            C:\>
            « Last Edit: June 12, 2009, 07:30:25 AM by billrich »

            billrich

            • Guest
            Re: dos for command
            « Reply #6 on: June 12, 2009, 06:51:29 AM »
            gkata,


            C:\>findstr /?
            Searches for strings in files.
            .
            .
            .

             
             For example : 'FINDSTR "hello there" x.txt' searches for "hello" or
            "there" in file x.txt.
            .
            .
            .

            C:\>

            gh0std0g74



              Apprentice

              Thanked: 37
              Re: dos for command
              « Reply #7 on: June 12, 2009, 06:53:31 AM »
              ...
              for /f "delims=*" %%A in (mainlog.txt) do (
              echo %%A  |  findstr  "ORA Record" >> newfile.txt
              ....
              the above method calls findstr for each line iteration. If there are 10000 lines, then findstr will be called 10000  times, which is inefficient.
              better to use it the other way round , ie
              Code: [Select]
              for .....  ( findstr "ORA Record" mainlog.txt ) do blah blah
              if there are further processing on each line found, or simply pipe to newfile if what OP wants is only to find ORA records
              Code: [Select]
              findstr [blah options] "text_to_find" >>newfile

              billrich

              • Guest
              Re: dos for command
              « Reply #8 on: June 12, 2009, 07:42:31 AM »
              ghost,

              You are right the for loop is  not needed:

              Code: [Select]
              @echo off
              echo. > newnewfile.txt
              findstr  "ORA Record"  mainlog.txt  >>  newnewfile.txt

              type  newnewfile.txt

              Output:


              C:\>type newnewfile.txt

              Record  bbbbbbbbbbbbbbbbbb
              ORA  xxxxxxxxxxxxxxxxxxxxxxxx

              C:\>

              p.s. Works just like the Unix grep command.

              « Last Edit: June 12, 2009, 08:02:14 AM by billrich »

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: dos for command
                « Reply #9 on: June 12, 2009, 10:11:03 AM »
                p.s. Works just like the Unix grep command.
                and also awk/sed. these tools iterate files and find patterns so they are similar. however, awk goes a bit more in that it is a programming language by itself. therefore, one does not need to learn grep/sed and use only awk to do the job( of many , eg cut, wc). this will reduce your learning curve a lot.