Solved Need to parse large conf files
-
@Obsolesce if schema is:
[Blabla]
; Comment line1
; Comment line 2
; Comment line 3
exten =>Yes you can ask for:
Grep -e "[.*]" -A 4Or similar: I'm in bed with my smartphone atm... Dont' remember the synthax
Grrr squared brackets have to be escaped but I can't...
-
@JaredBusch said in Need to parse large conf files:
@Pete-S said in Need to parse large conf files:
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.
-
@JaredBusch said in Need to parse large conf files:
And does all of that work with PHP 5.3
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.
-
@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.
There are many more contexts and many dial pans per context.
They are all (generally commented).
-
@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.
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?
-
Switching to PHP because the shell script is having issues, i assume because some characters are breakout out of the string variables.
-
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!
-
@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. -
Jared, will this do as a test file?
;; This is a test file [context_1] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Ben's inbound 2344242342 ; ; This extension is not suppose to go through ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; exten => 2344242342,2,NoOp() same => n,Goto(main,123456,1) same => n,Hangup() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Bob's inbound 3145551212 ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; exten => 3145551212,1,NoOp() same => n,Goto(main,123456,1) same => n,Hangup() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Mary's inbound 4534535345 ; ; With some added comments ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; exten => 4534535345,1,NoOp() same => n,Goto(main,123456,1) same => n,Hangup() [context_2] exten => 33333333,1,NoOp() same => n,Goto(main,123456,1) same => n,Hangup() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; What is this???????????? ; ; Lets add a long comment section ; ; Line 3,,, ; ; Line 4;Let's see if that comment remains ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; exten => 3145454,1,NoOp() same => n,Goto(main,123456,1) same => n,Hangup() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Let's make a wonky comment line here ; And lets add this too ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; exten => 232342,1,NoOp() same => n,Hangup() [context_3] exten => 7777777,1 exten => 8888888,1,NoOp() ;; Let's do these without comments
-
@Pete-S said in Need to parse large conf files:
Jared, will this do as a test file?
Close enough. I just moved your original sample code to the server as a starting point. (no PHP installed on my laptop to test locally).
-
@JaredBusch said in Need to parse large conf files:
@Pete-S said in Need to parse large conf files:
Jared, will this do as a test file?
Close enough. I just moved your original sample code to the server as a starting point. (no PHP installed on my laptop to test locally).
Let me just make some changes and test it on the test file. And I'll post it again.
PS. And what do you want the output to look like?
-
@JaredBusch said in Need to parse large conf files:
The catching the exten => STUFF,1 is the harder part. it HAS to be that full syntaxt.
no it is simple with regex IF the line is:
exten => bla,1,bla
try:
^exten => .*,1,.*$
-
@Pete-S said in Need to parse large conf files:
And what should the end result look like?
more or less this
[context 1] ; comment line -3 if it exists ; comment line -2 if it exists ; comment line -1 if it exists exten => STUFF,1,STUFF
repeat for dial plans and context appropriately.
-
@matteo-nunziati said in Need to parse large conf files:
@JaredBusch said in Need to parse large conf files:
The catching the exten => STUFF,1 is the harder part. it HAS to be that full syntaxt.
no it is simple with regex IF the line is:
exten => bla,1,bla
100% those are the only exten lines I want.
-
@JaredBusch said in Need to parse large conf files:
@Pete-S said in Need to parse large conf files:
And what should the end result look like?
more or less this
[context 1] ; comment line -3 if it exists ; comment line -2 if it exists ; comment line -1 if it exists exten => STUFF,1,STUFF
repeat for dial plans and context appropriately.
All the comment lines or just the last/first three?
-
@Pete-S said in Need to parse large conf files:
@JaredBusch said in Need to parse large conf files:
@Pete-S said in Need to parse large conf files:
And what should the end result look like?
more or less this
[context 1] ; comment line -3 if it exists ; comment line -2 if it exists ; comment line -1 if it exists exten => STUFF,1,STUFF
repeat for dial plans and context appropriately.
All the comment lines or just the last/first three?
i was just going with the up to three lines after the first visual scan of the files.
-
@JaredBusch said in Need to parse large conf files:
@matteo-nunziati said in Need to parse large conf files:
@JaredBusch said in Need to parse large conf files:
The catching the exten => STUFF,1 is the harder part. it HAS to be that full syntaxt.
no it is simple with regex IF the line is:
exten => bla,1,bla
100% those are the only exten lines I want.
Is it possible to have
exten => bla,2,bla
?? -
@Pete-S said in Need to parse large conf files:
@JaredBusch said in Need to parse large conf files:
@matteo-nunziati said in Need to parse large conf files:
@JaredBusch said in Need to parse large conf files:
The catching the exten => STUFF,1 is the harder part. it HAS to be that full syntaxt.
no it is simple with regex IF the line is:
exten => bla,1,bla
100% those are the only exten lines I want.
Is it possible to have
exten => bla,2,bla
??Yes, but I do not want them.
-
@JaredBusch said in Need to parse large conf files:
@matteo-nunziati said in Need to parse large conf files:
@JaredBusch said in Need to parse large conf files:
The catching the exten => STUFF,1 is the harder part. it HAS to be that full syntaxt.
no it is simple with regex IF the line is:
exten => bla,1,bla
100% those are the only exten lines I want.
@Pete-S if the php code works for @JaredBusch , just substitute the search part with the provided regex.
-
@JaredBusch Could you check that you can run command line php on the server?
Just executephp -v
and you'll get the version.At least on debian you need to have php-cli package and I don't know if you do.