大网数据广告活动
服务器运维问答

How to filter (out) certain output in bash

· 52CE 我爱测

I have a process that tails our AWS CloudWatch logs. It works great, except for the fact that every line is prepended by the CloudWatch group and container ID. For example: core-api 04a7ce3daf83aa018a92eb4a613c87354695dc8219f2136…

问题原文

原帖链接 · Taylor Halliday · CC BY-SA 3.0

I have a process that tails our AWS CloudWatch logs. It works great, except for the fact that every line is prepended by the CloudWatch group and container ID. For example:

core-api 04a7ce3daf83aa018a92eb4a613c87354695dc8219f213697f7f786b81d4d [PROD] - INFO: GET 200 - 5ms core-api 04a7ce3daf83aa018a92eb4a613c87354695dc8219f213697f7f786b81d4d [PROD] - INFO: POST 200 - 7ms

Is there a way to pipe my logs command I use with some type of filter to only show the line info after [PROD] -?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

You can try using cut to cut out the columns you are interested in.

tail -f whatever | cut -d ' ' -f 3-

[PROD] - INFO: GET 200 - 5ms
[PROD] - INFO: POST 200 - 7ms

This -d sets a space as the field delimiter, and -f specifies to display only the third and subsequent fields.

Specifying fields can get more complex, too. Suppose you only really want to get rid of that annoying useless second field. You can then specify -f 1,3-.

core-api [PROD] - INFO: GET 200 - 5ms
core-api [PROD] - INFO: POST 200 - 7ms

See the cut man page for more things you can do with this command.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。