Question

I need to extract Voip log from a D-Link router, so I've setup a little python script that executes a command in this router via telnet. My script does a "cat /var/log/calls.log" and returns the result, however... it also sends non-important stuff, like the BusyBox banner, etc... How can I ignore lines from 1 to 6 and the last 2 ? This is my current output:

yaba@foobar:/stuff$ python calls.py


BusyBox v1.00 (2009.04.09-11:17+0000) Built-in shell (msh)
Enter 'help' for a list of built-in commands.

DVA-G3170i/PT # cat /var/call.log
1         ,1294620563,2  ,+351xxx080806  ,xxx530802      ,1  ,3  ,1
DVA-G3170i/PT # exit

And I just need:

1         ,1294620563,2  ,+351xxx080806  ,xxx530802      ,1  ,3  ,1

(it can have multiple lines) So that I can save it to a CSV and later to a sql db.

Thanks, and sorry my bad english.

Was it helpful?

Solution

Why not use a pattern in AWK to match the text you want?

python calls.py | awk '/^[0-9]/{print}/'

The whole POINT of AWK is matching lines based on patterns and manipulating/printing those matched lines.


Edited to add example run.

Here's a junk data file based on your sample above.

$ cat junk.dat


BusyBox v1.00 (2009.04.09-11:17+0000) Built-in shell (msh)
Enter 'help' for a list of built-in commands.

DVA-G3170i/PT # cat /var/call.log
1         ,1294620563,2  ,+351xxx080806  ,xxx530802      ,1  ,3  ,1
DVA-G3170i/PT # exit

Here's running it through AWK with a filter.

$ cat junk.dat | awk '/^[0-9]/ {print}'
1         ,1294620563,2  ,+351xxx080806  ,xxx530802      ,1  ,3  ,1

No need for SED, no need for counting lines, no need for anything but AWK. Why make things more complicated than they need to be?

OTHER TIPS

In one call to sed:

sed -n '1,6d;7,${N;$q;P;D}'

or for picky versions of sed:

sed -ne '1,6d' -e '7,${N' -e '$q' -e 'P' -e 'D}'

You could also do it based on matches:

sed -n '/^[0-9]+/p'

or something similar.

But why doesn't your Python script read the file and do the filtering (instead of calling an external utility)?

python calls.py | sed -e 1,6d -e '$d'


So that might work. It will filter out the first 6 and the last, which is what your example indicates you need. If you really want to clobber the last two lines then you could do:

python calls.py | sed -e 1,6d -e '$d' | sed -e '$d'

But wait ... you said awk, so...

python calls.py | awk '{ if(NR > 7) { print t }; t = $0 }'

This might work for you:

sed '1,6d;$!N;$d;P;D' file

I'm not sure this is the best way to do it (maybe D-Link router has FTP or SSH support) but you can do it with awk:

awk '/cat/, /exit/' | sed -e '1d' -e '$d'

awk will print everything between lines containing "cat" and "exit", unfortunately including these two lines. That's what the remaining commands are for, I couldn't figure out how to do it nicer than that...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top