ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Solved Need to parse large conf files

    IT Discussion
    scripting asterisk config
    6
    53
    3.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch @Romo
      last edited by

      @Romo said in Need to parse large conf files:

      Python has configparser which handles this files, INI files.

      Maybe this would help you Jared - https://github.com/albfan/bash-ini-parser

      Neither are very helpful.

      1 Reply Last reply Reply Quote 0
      • JaredBuschJ
        JaredBusch @1337
        last edited by

        @Pete-S said in Need to parse large conf files:

        @JaredBusch

        Just to clarify, what is it you want to read from the config files, is it just the extension number?
        For example from exten => 3145551212,1,NoOp() to 3145551212 ?

        What should the end result be? A new file with all the extension numbers?

        For now, I want the comments and the exten line.

        Once i have it in a form I can analyze, i will be writing more statements to do specific things.

        1 Reply Last reply Reply Quote 0
        • JaredBuschJ
          JaredBusch
          last edited by

          Went and had some lunch. Getting back on this now.

          ObsolesceO 1 2 Replies Last reply Reply Quote 0
          • ObsolesceO
            Obsolesce @JaredBusch
            last edited by

            @JaredBusch said in Need to parse large conf files:

            Went and had some lunch. Getting back on this now.

            To me it seems easier to do in PowerShell. If I could get something together sometime, would you consider it? But I'll need more data to work with for testing.

            JaredBuschJ 1 Reply Last reply Reply Quote 0
            • JaredBuschJ
              JaredBusch @Obsolesce
              last edited by

              @Obsolesce said in Need to parse large conf files:

              @JaredBusch said in Need to parse large conf files:

              Went and had some lunch. Getting back on this now.

              To me it seems easier to do in PowerShell. If I could get something together sometime, would you consider it? But I'll need more data to work with for testing.

              What good would that do me when the OS is Ubuntu 12.04?

              ObsolesceO scottalanmillerS 2 Replies Last reply Reply Quote 0
              • ObsolesceO
                Obsolesce @JaredBusch
                last edited by

                @JaredBusch said in Need to parse large conf files:

                @Obsolesce said in Need to parse large conf files:

                @JaredBusch said in Need to parse large conf files:

                Went and had some lunch. Getting back on this now.

                To me it seems easier to do in PowerShell. If I could get something together sometime, would you consider it? But I'll need more data to work with for testing.

                What good would that do me when the OS is Ubuntu 12.04?

                My thought was that you could grab the file and run it on something with PS.

                Or is this something that needs to run on the system? Perhaps it'll work with PS6.1?

                1 Reply Last reply Reply Quote 0
                • scottalanmillerS
                  scottalanmiller @JaredBusch
                  last edited by

                  @JaredBusch said in Need to parse large conf files:

                  @Obsolesce said in Need to parse large conf files:

                  @JaredBusch said in Need to parse large conf files:

                  Went and had some lunch. Getting back on this now.

                  To me it seems easier to do in PowerShell. If I could get something together sometime, would you consider it? But I'll need more data to work with for testing.

                  What good would that do me when the OS is Ubuntu 12.04?

                  Will PS install via AppImage in 12.04? That's so old, not sure what it can do.

                  1 Reply Last reply Reply Quote 0
                  • 1
                    1337 @JaredBusch
                    last edited by 1337

                    @JaredBusch

                    It's faster to do this programmatically than trying to piece it together with small utilities and scripting glue, especially if you want to store the results in a database or something like that.

                    I'll write some pseudo correct php code for you here to show what I mean.

                    // find matching files, put in array called filename
                    $filename:=glob("*.ini");
                    
                    // scan through all the files
                    foreach ($filename as $f) {
                    
                       // read all lines into lines array
                       $lines:=file($f);
                    
                       // go through the file and pick out what we need
                       $line:=0; // linenumber
                       foreach ($lines as $line) do {
                          $line++;
                    
                          // check the start of each line to look for "exten =>"
                          $search:="exten =>";
                          if substr($line,0,strlen($search))==$search {
                             // we found the extension row
                             // lets take everything after "exten =>"
                             $extline:=substr($line,strlen($search));
                             // lets divide it up 
                             $parts:=explode($extline, ",");
                             $ext:=$parts[0]; // extension number
                    
                             // lets pick out the three preceeding lines
                             $comment[1]:=$lines[$line-3];
                             $comment[2]:=$lines[$line-2];
                             $comment[3]:=$lines[$line-1];
                    
                             // do more stuff on the line we picked out here
                            
                             // lets save what we might need in an array
                             $extensions[$ext]:=array("comments"=>$comment, "linenumber"=>$line, "file"=>$f);
                          }
                          
                          // search for other stuff and what not
                       }
                       
                       // do more stuff on the file
                    }
                    
                    // remove comment to print array for inspection/debug
                    //print_r($extensions);
                    
                    // lets print out all extension found in all files
                    foreach ($extensions as $ext => $data) do {
                       print "Extension: $ext\r\n";
                    }
                    
                    
                    JaredBuschJ 1 Reply Last reply Reply Quote 0
                    • JaredBuschJ
                      JaredBusch @1337
                      last edited by

                      @Pete-S said in Need to parse large conf files:

                      @JaredBusch

                      It's faster to do this programmatically than trying to piece it together with small utilities and scripting glue, especially if you want to store the results in a database or something like that.

                      I'll write some pseudo correct php code for you here to show what I mean.

                      // find matching files, put in array called filename
                      $filename:=glob("*.ini");
                      
                      // scan through all the files
                      foreach ($filename as $f) {
                      
                         // read all lines into lines array
                         $lines:=file($f);
                      
                         // go through the file and pick out what we need
                         $line:=0; // linenumber
                         foreach ($lines as $line) do {
                            $line++;
                      
                            // check the start of each line to look for "exten =>"
                            $search:="exten =>";
                            if substr($line,0,strlen($search))==$search {
                               // we found the extension row
                               // lets take everything after "exten =>"
                               $extline:=substr($line,strlen($search));
                               // lets divide it up 
                               $parts:=explode($extline, ",");
                               $ext:=$parts[0]; // extension number
                      
                               // lets pick out the three preceeding lines
                               $comment[1]:=$lines[$line-3];
                               $comment[2]:=$lines[$line-2];
                               $comment[3]:=$lines[$line-1];
                      
                               // do more stuff on the line we picked out here
                              
                               // lets save what we might need in an array
                               $extensions[$ext]:=array("comments"=>$comment, "linenumber"=>$line, "file"=>$f);
                            }
                            
                            // search for other stuff and what not
                         }
                         
                         // do more stuff on the file
                      }
                      
                      // remove comment to print array for inspection/debug
                      //print_r($extensions);
                      
                      // lets print out all extension found in all files
                      foreach ($extensions as $ext => $data) do {
                         print "Extension: $ext\r\n";
                      }
                      
                      

                      And does all of that work with PHP 5.3

                      9a35546f-ac40-450d-93fb-859071c04aa1-image.png

                      1 JaredBuschJ 2 Replies Last reply Reply Quote 0
                      • JaredBuschJ
                        JaredBusch
                        last edited by

                        @Pete-S said in Need to parse large conf files:

                        @JaredBusch

                        It's faster to do this programmatically than trying to piece it together with small utilities and scripting glue, especially if you want to store the results in a database or something like that.

                        Since when is a bash script not doing something programmatically?

                        matteo nunziatiM 1 2 Replies Last reply Reply Quote 0
                        • matteo nunziatiM
                          matteo nunziati @JaredBusch
                          last edited by

                          @JaredBusch sorry I'm missing something here... Isn't this something you can do with grep? If you know how your line begins you can ask grep to find that line and some context before and after that line.

                          ObsolesceO JaredBuschJ 2 Replies Last reply Reply Quote 0
                          • ObsolesceO
                            Obsolesce @matteo nunziati
                            last edited by

                            @matteo-nunziati said in Need to parse large conf files:

                            @JaredBusch sorry I'm missing something here... Isn't this something you can do with grep? If you know how your line begins you can ask grep to find that line and some context before and after that line.

                            Like each line that begins with ; and exten?

                            matteo nunziatiM 1 Reply Last reply Reply Quote 0
                            • matteo nunziatiM
                              matteo nunziati @Obsolesce
                              last edited by matteo nunziati

                              @Obsolesce if schema is:
                              [Blabla]
                              ; Comment line1
                              ; Comment line 2
                              ; Comment line 3
                              exten =>

                              Yes you can ask for:
                              Grep -e "[.*]" -A 4

                              Or similar: I'm in bed with my smartphone atm... Dont' remember the synthax

                              Grrr squared brackets have to be escaped but I can't...

                              1 Reply Last reply Reply Quote 0
                              • 1
                                1337 @JaredBusch
                                last edited by

                                @JaredBusch said in Need to parse large conf files:

                                @Pete-S said in Need to parse large conf files:

                                @JaredBusch

                                It's faster to do this programmatically than trying to piece it together with small utilities and scripting glue, especially if you want to store the results in a database or something like that.

                                Since when is a bash script not doing something programmatically?

                                Well, bash is programs of course a program but using grep for instance you are defining search patterns and using command line options to get it to pick out specific lines. It's not the same as writing a program that would go back and forth in the files and pick out what you need and put it together anyway you like.

                                1 Reply Last reply Reply Quote 0
                                • 1
                                  1337 @JaredBusch
                                  last edited by 1337

                                  @JaredBusch said in Need to parse large conf files:

                                  And does all of that work with PHP 5.3

                                  9a35546f-ac40-450d-93fb-859071c04aa1-image.png

                                  In general yes (you just run it with php filename.php) but I'd have to have some files and debug it to know for sure. I just wrote it on the top of my head as an example just as I'm writing this.

                                  You might favor another language. Python, java, javascript, what do I know.

                                  1 Reply Last reply Reply Quote 0
                                  • JaredBuschJ
                                    JaredBusch @matteo nunziati
                                    last edited by

                                    @matteo-nunziati said in Need to parse large conf files:

                                    @JaredBusch sorry I'm missing something here... Isn't this something you can do with grep? If you know how your line begins you can ask grep to find that line and some context before and after that line.

                                    The is what is needed.

                                    4e10f724-9c5c-4c6e-8a2e-d85779c58b69-image.png

                                    There are many more contexts and many dial pans per context.

                                    They are all (generally commented).

                                    1 1 Reply Last reply Reply Quote 0
                                    • 1
                                      1337 @JaredBusch
                                      last edited by 1337

                                      @JaredBusch said in Need to parse large conf files:

                                      @matteo-nunziati said in Need to parse large conf files:

                                      @JaredBusch sorry I'm missing something here... Isn't this something you can do with grep? If you know how your line begins you can ask grep to find that line and some context before and after that line.

                                      The is what is needed.

                                      4e10f724-9c5c-4c6e-8a2e-d85779c58b69-image.png

                                      There are many more contexts and many dial pans per context.

                                      They are all (generally commented).

                                      So basically all extension numbers and all preceding comment lines for those extensions under each [context]?

                                      And what should the end result look like?

                                      JaredBuschJ 1 Reply Last reply Reply Quote 0
                                      • JaredBuschJ
                                        JaredBusch
                                        last edited by

                                        Switching to PHP because the shell script is having issues, i assume because some characters are breakout out of the string variables.

                                        1 1 Reply Last reply Reply Quote 0
                                        • matteo nunziatiM
                                          matteo nunziati
                                          last edited by

                                          quite late here... sorry I've just an half backed solution:

                                           grep -e "^exten\|^[\|^;;" filename
                                          

                                          this doesn't retain any structure but extracts any line beginning with [...] or exten or ;;
                                          unfortunately comments are shown regardless of their usefulness...

                                          maybe pocking with awk would lead to better results... but I really need to sleep!

                                          JaredBuschJ 1 Reply Last reply Reply Quote 0
                                          • JaredBuschJ
                                            JaredBusch @matteo nunziati
                                            last edited by JaredBusch

                                            @matteo-nunziati said in Need to parse large conf files:

                                            quite late here... sorry I've just an half backed solution:

                                             grep -e "^exten\|^[\|^;;" filename
                                            

                                            That is the ballpark, but many lines are comments that are not needed.

                                            I was simply going to loop down the file each time through the loop putting the previosu line back one vairable. that part was simple.

                                            The catching the exten => STUFF,1 is the harder part. it HAS to be that full syntaxt.

                                            matteo nunziatiM 1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 1 / 3
                                            • First post
                                              Last post