How to filter (out) certain output in bash

Taylor Halliday asked:

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 ex:

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] -“?

My answer:

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.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.