Scan line and extract specific info

This topic contains 2 replies, has 2 voices, and was last updated by  Rakesh 5 days, 7 hours ago.

  • Author
    Posts
  • #100722

    Rakesh
    Participant

    Hello Everyone,

    I need to create a script which can scan through a config file, then extract some specific info and write them into new files.
    I am still a beginner, so it may appear to be an easy thing to most. I appreciate any kind of help you can offer on this.

    Below are few lines from the input file:

    BEGIN INITIALIZATION REGION=HALOC010
    START REGION=HALOC010
    BEGIN EXECUTE LCAQPRO1
    BEGIN EXECUTE LCAQPRO2 GROUP=OACI410G
    BEGIN EXECUTE PROGRAM=ICP9120 GROUP=KEE202
    BEGIN EXECUTE PROGRAM=ICP9121 GROUP=KEE202

    I need to list out the programs and scripts from the config file and create two new files. (Scripts are followed by the word 'EXECUTE ' and programs are the ones followed by 'PROGRAM=')

    File#1 should look like
    LCAQPRO1
    LCAQPRO2

    File#2 should look like
    ICP9120

    Here is how I am trying to do, but it's definitely not the right way. And the config file can have any number spaces between each word.

    cat conf.file | where { $_ -match "EXECUTE"} > temp1.file
    cat temp1.file | where { $_ -match "PROGRAM"} > temp2.file
    type temp1.file | findstr /V /C:"PROGRAM" > temp3.file
    Get-Content temp3.file | ForEach-Object { 
        $_.Split("EXECUTE", [System.StringSplitOptions]::RemoveEmptyEntries) 
    } | Set-Content temp4.file
    

    Thanks.
    Rakesh

  • #100731

    Mike R.
    Participant

    The code below is a little clunky but should work for you, barring any other format anomalies in your conf file. If you use -split in front of the string it uses the default of any white space so the number of spaces and/or tabs shouldn't affect it.

    $conffile = "config.file" #use full or relative paths if not in working dir
    $temp1 = "temp1.file"
    $temp2 = "temp2.file"
    
    $conf = Get-Content -Path $conffile 
    $conf | % {
        if($_.Contains("EXECUTE PROGRAM=")) {
            (-split ($_ -Split("=")))[3] | Out-File -FilePath $temp2 -Append
        }
        elseif($_.contains("EXECUTE")) {
            (-split $_)[2] | Out-File -FilePath $temp1 -Append
        }
    }
  • #100765

    Rakesh
    Participant

    Thanks for your reply. It does work, however the data in the cofig file is not very well formatted. There are multiple spaces, comma and semicolon at random places. I am trying to get rid of them first, and then will use the code you shared.

You must be logged in to reply to this topic.