locked
Looking for something in text file RRS feed

  • Question

  • Hello -- Real rookie here writing powershell scripts.  I'm looking to search through text files in a folder for the phrase "Zero-point correction="

    I then what to return a number that appears on the same line near the phrase "Zero-point correction=" and what file that was found in.

    Here's what I have so far:

    $PATH = "C:\G03W\Angie\Au dimer\B3LYP\Au2 -1"


    dir "$PATH" -filter *.log -recurse | select-string "Zero-point correction="| select Line,Filename  | Format-Table -auto -wrap | out-file "$PATH\zerpoints.txt"

     

    My output then looks like this:


    Line                                                                          Filename                                
    ----                                                                          --------                                
     Zero-point correction=                           0.000272 (Hartree/Particle) AU2 -1 DOUBLET OPT+FREQ.LOG             
     Zero-point correction=                           0.006180 (Hartree/Particle) AU2CO -1 DOUBLET L-DESIGN SDD-6311+G2DPOS
                                                                                  TSTABLE OPT.LOG                         
     Zero-point correction=                           0.004519 (Hartree/Particle) AU2O2 -1 DOUBLET L-DESIGN SDD-6311+G2DPOS
                                                                                  TSTABLE OPTFREQ.LOG                     
     Zero-point correction=                           0.004730 (Hartree/Particle) AU2O2 -1 DOUBLET L-DESIGN SDD-6311+G2DPOS
                                                                                  TSTABLE OPT+FREQ.LOG                    
     Zero-point correction=                           0.011356 (Hartree/Particle) AU2CO O2 -1 DOUBLET ANGLED SDD-6311+G2DPO
                                                                                  STSTABLE OPTFREQ.LOG                    
     Zero-point correction=                           0.011356 (Hartree/Particle) AU2CO O2 -1 DOUBLET ANGLED SDD-6311+G2DPO
                                                                                  STSTABLE OPTFREQ READIN.LOG             
     Zero-point correction=                           0.013124 (Hartree/Particle) O2AU2CO -1 DOUBLET LANDMANC SDD-6311+G2D
                                                                                  OPT+FREQB3LYP.LOG                       
     Zero-point correction=                           0.013178 (Hartree/Particle) O2AU2CO -1 DOUBLET LANDMANCV2 SDD-6311+G2
                                                                                  D OPT+FREQB3LYP.LOG                     


    What I really want is the numbers and the file name in a format I can easily move into excel.  One problem is there are a ton of spaces between the Zero-point correction= and the numbers.  Also after the numbers (Hartree/Particle) appears.  The spaces comes from the files being searched through (they have spaces).

    I've experimented with searching and trimming the spaces from the powershell results file with some success but I lose the table formating and the log file names get broken up across lines breaking my ability to easily import them to excel.

    Can someone point me in the right direction?  There's probably an easier way to search this out.

     

    Saturday, February 4, 2012 3:30 AM

Answers

  • dir $path -filter *.log -recurse | 
         select-string 'Zero-point correction=' | 
         select @{N='Number';E={$_.line.SubString(48,9)}},  filename

    Try that.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by IamMred Sunday, February 12, 2012 5:30 AM
    Thursday, February 9, 2012 2:10 AM

All replies

  • Please post a sample of your text file.
    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Network Live Audit - Powershell script
    Saturday, February 4, 2012 12:25 PM
  • dir $path -filter *.log -recurse | 
         select-string 'Zero-point correction=' | 
         select @{N='Number';E={$_.line.SubString(0,8)}},  filename
    


    ¯\_(ツ)_/¯
    Saturday, February 4, 2012 6:22 PM
  • I must be making a mistake somewhere: 

    When I run

    dir "$PATH" -filter *.log -recurse | select-string "Zero-point correction="| select @{N='Number';E={$_.line.SubString(0,8)}}, filename | Format-Table -auto -wrap | out-file "$PATH\zerpoints.txt"

    I'm getting my zero point (the first part of the line) rather than the number.  I'm a real novice on this but obviously I assume I did something wrong in the select statement. 

    Number   Filename                                                            
    ------   --------                                                            
     Zero-po AU2 -1 DOUBLET OPT+FREQ.LOG                                         
     Zero-po AU2CO -1 DOUBLET L-DESIGN SDD-6311+G2DPOSTSTABLE OPT.LOG            
     Zero-po AU2O2 -1 DOUBLET L-DESIGN SDD-6311+G2DPOSTSTABLE OPTFREQ.LOG        
     Zero-po AU2O2 -1 DOUBLET L-DESIGN SDD-6311+G2DPOSTSTABLE OPT+FREQ.LOG       
     Zero-po AU2CO O2 -1 DOUBLET ANGLED SDD-6311+G2DPOSTSTABLE OPTFREQ.LOG       
     Zero-po AU2CO O2 -1 DOUBLET ANGLED SDD-6311+G2DPOSTSTABLE OPTFREQ READIN.LOG
     Zero-po O2AU2CO -1 DOUBLET LANDMANC SDD-6311+G2D OPT+FREQB3LYP.LOG          
     Zero-po O2AU2CO -1 DOUBLET LANDMANCV2 SDD-6311+G2D OPT+FREQB3LYP.LOG      

     Here's an section thats in the text files I'm searching (too large to paste and I haven't figure out how to attach it yet) that contains a Zero-Point Correction Number that I want to return. 

    -------------------
     - Thermochemistry -
     -------------------
     Temperature   298.150 Kelvin.  Pressure   1.00000 Atm.
     Atom  1 has atomic number 79 and mass 196.96660
     Atom  2 has atomic number 79 and mass 196.96660
     Molecular mass:   393.93320 amu.
     Principal axes and moments of inertia in atomic units:
                               1         2         3
         EIGENVALUES --     0.000002606.068542606.06854
               X            0.00000   1.00000   0.00000
               Y            0.00000   0.00000   1.00000
               Z            1.00000   0.00000   0.00000
     This molecule is a prolate symmetric top.
     Rotational symmetry number  2.
     Warning -- assumption of classical behavior for rotation
               may cause significant error
     Rotational temperature (Kelvin)      0.03324
     Rotational constant (GHZ):           0.692515
     Zero-point vibrational energy        713.0 (Joules/Mol)
                                        0.17042 (Kcal/Mol)
     Warning -- explicit consideration of   1 degrees of freedom as
               vibrations may cause significant error
     Vibrational temperatures:    171.52
              (Kelvin)
     
     Zero-point correction=                           0.000272 (Hartree/Particle)
     Thermal correction to Energy=                    0.003331
     Thermal correction to Enthalpy=                  0.004275
     Thermal correction to Gibbs Free Energy=        -0.027555
     Sum of electronic and zero-point Energies=           -271.650974
     Sum of electronic and thermal Energies=              -271.647915
     Sum of electronic and thermal Enthalpies=            -271.646971
     Sum of electronic and thermal Free Energies=         -271.678801
     
                         E (Thermal)             CV                S
                          KCal/Mol        Cal/Mol-Kelvin    Cal/Mol-Kelvin
     Total                    2.090              6.901             66.992
     Electronic               0.000              0.000              1.377
     Translational            0.889              2.981             43.805
     Rotational               0.592              1.987             18.697
     Vibrational              0.609              1.933              3.113

    ....

    Thursday, February 9, 2012 2:02 AM
  • dir $path -filter *.log -recurse | 
         select-string 'Zero-point correction=' | 
         select @{N='Number';E={$_.line.SubString(48,9)}},  filename

    Try that.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by IamMred Sunday, February 12, 2012 5:30 AM
    Thursday, February 9, 2012 2:10 AM
  • Thanks, that seemed to do it.  I assume the select statement is just selecting X characters starting Y length into the the string?

    One follow up question:  Is there a reasonable way to search through both .log and .out's with this script or would it be better to just run the query again on the path w/ .out as the filter instead?

    dir "$PATH" -filter *.log -recurse | select-string "Zero-point correction="| select @{N='Number';E={$_.line.SubString(0,8)}}, filename | Format-Table -auto -wrap | out-file "$PATH\zerpoints.txt"

    thanks!

    Thursday, February 9, 2012 2:23 AM
  • Thanks, that seemed to do it.  I assume the select statement is just selecting X characters starting Y length into the the string?

    One follow up question:  Is there a reasonable way to search through both .log and .out's with this script or would it be better to just run the query again on the path w/ .out as the filter instead?

    dir "$PATH" -filter *.log -recurse | select-string "Zero-point correction="| select @{N='Number';E={$_.line.SubString(0,8)}}, filename | Format-Table -auto -wrap | out-file "$PATH\zerpoints.txt"

    thanks!

    Yep. The first argument to the .substring() method is starting position, second is length.

    Not sure how to get multiple filespecs with along with the path parameter.  If they're all in the same directory, you can do

    dir *.log,*.out

    from that directory and get them both.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 9, 2012 2:42 AM
  • The following works, and can be combined with what you have:

    dir *.log,*.out

    -----

    It does not work with -Filter.


    Richard Mueller - MVP Directory Services

    Thursday, February 9, 2012 2:48 AM
  • So replace

    dir "$PATH" -Filter *.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table -auto -wrap | out-file "$PATH\List of Stable Files.txt"

    with

    dir "$PATH" *.log,*.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table -auto -wrap | out-file "$PATH\List of Stable Files.txt"  ??

    That seems to die

    Get-ChildItem : A parameter cannot be found that matches parameter name 'System.Object[]'.
    At C:\G09W\Angie\stablecheck.ps1:6 char:4
    + dir  <<<< "$PATH" *.log,*.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table
     -auto -wrap | out-file "$PATH\List of Stable Files.txt"

    If I just duplicated my command(changing out to log)

    dir "$PATH" -Filter *.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table -auto -wrap | out-file "$PATH\List of Stable Files.txt"

    I would need to change my out to something that appends rather than overwrites the file.  EDIT a little googling and i leanrd about the drop dead simple -append command if i do my second example option.


    Thursday, February 9, 2012 3:21 AM
  • So replace

    dir "$PATH" -Filter *.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table -auto -wrap | out-file "$PATH\List of Stable Files.txt"

    with

    dir "$PATH" *.log,*.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table -auto -wrap | out-file "$PATH\List of Stable Files.txt"  ??

    That seems to die

    Get-ChildItem : A parameter cannot be found that matches parameter name 'System.Object[]'.
    At C:\G09W\Angie\stablecheck.ps1:6 char:4
    + dir  <<<< "$PATH" *.log,*.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table
     -auto -wrap | out-file "$PATH\List of Stable Files.txt"

    If I just duplicated my command(changing out to log)

    dir "$PATH" -Filter *.out -recurse | select-string "wavefunction is stable under"| select Filename | Format-Table -auto -wrap | out-file "$PATH\List of Stable Files.txt"

    I would need to change my out to something that appends rather than overwrites the file.  EDIT a little googling and i leanrd about the drop dead simple -append command if i do my second example option.


    Are you trying to set a folder and a filter?

    Dir  -path c:\test -filter *.log

    Simple stuff for us Ents.  Bilbo would be proud!


    ¯\_(ツ)_/¯

    Thursday, February 9, 2012 3:47 AM
  • Trying to set a folder and an array of filters. 

    What say thee, Treebeard?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 9, 2012 3:53 AM
  • Trying to set a folder and an array of filters. 

    What say thee, Treebeard?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Treebeard was my father but thank you for the recognition. I am in no way his equal.

    Aye - these folders are a bit of it. The filters remind me of the makin' of the grog.

    We must pay this out in bits and pieces. 

    I 'spect we need an out.

    Treebeard actually survived and created a while new generation.  I am the begining of that creation.


    ¯\_(ツ)_/¯

    Thursday, February 9, 2012 4:44 AM
  • Get-ChildItem  -Path $path -Include '*.log', '*.out'


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Proposed as answer by jrv Thursday, February 9, 2012 5:30 AM
    Thursday, February 9, 2012 5:13 AM
  • Get-ChildItem -Path $path -Include '*.log', '*.out'


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    You know it works like this.

    Everywhere in PowerShell we can post arrays and collections to the CmdLets.

    Think of DOS and VBscript. Think of how limited the responses are. PowerShell can take compound and complex statements for many if not most arguments.

    Put that in your smoke and pipe it!


    ¯\_(ツ)_/¯

    Thursday, February 9, 2012 5:37 AM
  • Yes, we can be very specific, and do complex queries with one singe Get-ChildItem command.  For example:

    Get-ChildItem  `
        -Path 'C:\Scripts\*','c:\*' `
        -Include '*.log', '*.out' `
        -Filter 's*' `
        -Exclude 'server3.log'

    Here we are searching 2 distinct paths for specific extensions, with names that begin with "S", and we are excluding a specific file (perhaps this is the current log, and is in use).


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, February 9, 2012 6:56 AM
  • I think the code coloring is just completely random.  We get three colors: Purple, green, and black.  And they just seem to color the code in a random fashion.  It's so ugly.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, February 9, 2012 6:58 AM
  • You have to remember that if you switch from using -filter to using -include you have to go back and change $path to use a wildcard if it isn't already.

    It will work with -filter without the wildcard in the -path argument, but -include doesn't work without it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Thursday, February 9, 2012 11:48 AM
    Thursday, February 9, 2012 11:46 AM
  • You have to remember that if you switch from using -filter to using -include you have to go back and change $path to use a wildcard if it isn't already.

    It will work with -filter without the wildcard in the -path argument, but -include doesn't work without it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "



    True, I have wildcards in my paths.  But you don't need them if you specify -Recurse.  Get-ChildItem is quite strange.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, February 9, 2012 11:53 AM
  • Just switching from filter (which only takes [string]) to -include and using an array of arguments fails with in this case, because $path didn't have the wildcard.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 9, 2012 12:00 PM
  • Yes, I know.  I wasn't looking too carefully at the contents of $path, I was more just trying to point out how we can look for more than one extension with a single command.

    If you see my second example, with literals, I have included a \* at the end of each path, for this very reason.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Thursday, February 9, 2012 12:08 PM
    Thursday, February 9, 2012 12:07 PM