awk 和 cat - 如何忽略多行?

发布于 2024-10-11 01:08:41 字数 658 浏览 5 评论 0原文

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

嗼ふ静 2024-10-18 01:08:41

为什么不在 AWK 中使用模式来匹配您想要的文本?

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

AWK 的整个要点是根据模式匹配行并操作/打印这些匹配的行。


已编辑以添加示例运行。

这是基于上面示例的垃圾数据文件。

$ 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

这里使用过滤器通过 AWK 运行它。

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

不需要 SED,不需要计算行数,除了 AWK 之外不需要任何东西。为什么要让事情变得比需要的更复杂呢?

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?

时光磨忆 2024-10-18 01:08:41

在对 sed: 的一次调用中,

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

或者对于 sed: 的挑剔版本:

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

您也可以根据 matches:

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

或类似的东西来执行此操作。

但是为什么您的 Python 脚本不读取文件并进行过滤(而不是调用外部实用程序)?

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)?

下壹個目標 2024-10-18 01:08:41

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


所以这可能有效。它将过滤掉前 6 个和最后一个,这就是您的示例表明您需要的。如果你真的想破坏最后两行,那么你可以这样做:

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

但是等等......你说的是 awk,所以......

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

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 }'
绮烟 2024-10-18 01:08:41

这可能对你有用:

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

This might work for you:

sed '1,6d;$!N;$d;P;D' file
亣腦蒛氧 2024-10-18 01:08:41

我不确定这是最好的方法(也许 D-Link 路由器有 FTP 或 SSH 支持),但你可以使用 awk 来完成:

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

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 '/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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文