S
S
Sergey2020-05-20 15:29:37
linux
Sergey, 2020-05-20 15:29:37

How to highlight certain words in the output of a bash script?

Tell me please.
I want to write a simple script to make it easier to parse the iptables trace.

#!/bin/bash
tail /var/log/syslog -n $1 | cut -d" " -f8-10,12-13,20-22,31

Here's what happens
filter:FORWARD:rule:6 IN=eth2 OUT=eth0 SRC=91.210.170.11 DST=10.200.2.2 PROTO=TCP SPT=40860 DPT=22 MARK=0x210014
filter:WWW_SERV_FILTER:rule:17 IN=eth2 OUT=eth0 SRC=91.210.170.11 DST=10.200.2.2 PROTO=TCP SPT=40860 DPT=22 MARK=0x210014
filter:FORWARD_ACCEPT:rule:1 IN=eth2 OUT=eth0 SRC=91.210.170.11 DST=10.200.2.2 PROTO=TCP SPT=40860 DPT=22 MARK=0x210014
raw:PREROUTING:policy:3 IN=eth2 OUT= SRC=91.210.170.11 DST=178.217.153.77 PROTO=TCP SPT=40860 DPT=10022
mangle:PREROUTING:rule:6 IN=eth2 OUT= SRC=91.210.170.11 DST=178.217.153.77 PROTO=TCP SPT=40860 DPT=10022
mangle:WWW_SERV:rule:1 IN=eth2 OUT= SRC=91.210.170.11 DST=178.217.153.77 PROTO=TCP SPT=40860 DPT=10022
mangle:WWW_SERV:rule:3 IN=eth2 OUT= SRC=91.210.170.11 DST=178.217.153.77 PROTO=TCP SPT=40860 DPT=10022
mangle:WWW_SERV:return:5 IN=eth2 OUT= SRC=91.210.170.11 DST=178.217.153.77 PROTO=TCP SPT=40860 DPT=10022

It outputs in this form, but so that it is not just a black and white sheet, I want to highlight the keywords in different colors. (table names raw, mangle, nat, filter and fields IN, OUT, SRC, DST, etc.)
Tell me how this can be done?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2020-05-20
@PATRIOT

#!/bin/bash
tail /var/log/syslog -n $1 | cut -d" " -f8-10,12-13,20-22,31 | \
sed '
s/filter/\o33[32m&\o033[0m/g;
s/mangle/\o33[33m&\o033[0m/g;
s/raw/\o33[31m&\o033[0m/g;
s/nat/\o33[35m&\o033[0m/g;
s/OUT/\o033[1m&\o033[0m/g;.
s/IN/\o33[1m&\o033[0m/g;.
s/PROTO/\o33[1m&\o033[0m/g;.
s/MARK/\o33[1m&\o033[0m/g;.
s/DPT/\o33[1m&\o033[0m/g;.
s/SPT/\o33[1m&\o033[0m/'

5ec532f30e89d337340660.png
Yes, it worked, thanks

P
pfg21, 2020-05-20
@pfg21

google "color in bash" kua descriptions.
https://habr.com/ru/post/119436/

S
shurshur, 2020-05-20
@shurshur

echo -e '\e[31;1mПривет!\e[0m'
See man console_codes for details.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question