awk 和 cat - 如何忽略多行?
我需要从 D-Link 路由器中提取 Voip 日志,因此我设置了一个小 python 脚本,用于通过 telnet 在此路由器中执行命令。 我的脚本执行“cat /var/log/calls.log”并返回结果,但是...... 它还发送不重要的内容,例如 BusyBox 横幅等... 如何忽略第 1 到 6 行以及最后 2 行? 这是我当前的输出:
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
我只需要:(
1 ,1294620563,2 ,+351xxx080806 ,xxx530802 ,1 ,3 ,1
它可以有多行) 这样我就可以将其保存到 CSV 中,然后再保存到 sql 数据库中。
谢谢,抱歉我的英语不好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不在 AWK 中使用模式来匹配您想要的文本?
AWK 的整个要点是根据模式匹配行并操作/打印这些匹配的行。
已编辑以添加示例运行。
这是基于上面示例的垃圾数据文件。
这里使用过滤器通过 AWK 运行它。
不需要 SED,不需要计算行数,除了 AWK 之外不需要任何东西。为什么要让事情变得比需要的更复杂呢?
Why not use a pattern in AWK to match the text you want?
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.
Here's running it through AWK with a filter.
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?
在对
sed
: 的一次调用中,或者对于
sed
: 的挑剔版本:您也可以根据 matches:
或类似的东西来执行此操作。
但是为什么您的 Python 脚本不读取文件并进行过滤(而不是调用外部实用程序)?
In one call to
sed
:or for picky versions of
sed
:You could also do it based on matches:
or something similar.
But why doesn't your Python script read the file and do the filtering (instead of calling an external utility)?
python 调用.py | sed -e 1,6d -e '$d'
所以这可能有效。它将过滤掉前 6 个和最后一个,这就是您的示例表明您需要的。如果你真的想破坏最后两行,那么你可以这样做:
但是等等......你说的是 awk,所以......
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:
But wait ... you said awk, so...
这可能对你有用:
This might work for you:
我不确定这是最好的方法(也许 D-Link 路由器有 FTP 或 SSH 支持),但你可以使用 awk 来完成:
awk 将打印包含“cat”和“exit”的行之间的所有内容,不幸的是包括这两行。这就是剩下的命令的用途,我不知道如何做得更好......
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 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...