Jakob
Jakob

Reputation: 23

AWK print and color 3 variables

I have two AWK commands that do the equivalent of grep -e PATTERNand the other colors specific words in the output but doesn't filter only those lines. Can they be combined into one awk.sh file?

This one prints 3 variables (Username, Group Policy, and Assigned IP) :

awk '/^Username/{print $0}/^Group/{print $0}/^Assigned/{print $0}' session.log

So it looks like this:

[root@localhost User]# ./find.sh Username : User1 Index : 111 Assigned IP : 11.11.11.111 Public IP : 22.22.22.222 Group Policy : DfltGrpPolicy Tunnel Group : Default-VPN Username : User2 Index : 111 Assigned IP : 11.11.11.111 Public IP : 22.22.22.222 Group Policy : DfltGrpPolicy Tunnel Group : Default-VPN

The other colors those variables green and red respectively (but outputs a lot of junk, and for some reason the coloring is broken on the second variable?):

cat session.log | awk '{ gsub("Username", "\033[1;32m&\033[0m");
                                   gsub("Assigned IP", "\033[1;32m&\032[0m");
                                   gsub("Group Policy", "\033[1;31m&\033[0m");
                                   print }'

Here is the picture of colouring output:

Picture of coloring output

And here is the edited in MS Word, but this is my end goal.

Edited in MS Word, but this is my end goal.

Upvotes: 1

Views: 241

Answers (1)

steffen
steffen

Reputation: 16948

  1. You don't need cat to read the file. Just use awk.
  2. I think you want to sub instead of gsub.
  3. In Assigned IP, you have \032[0m but I guess you wanted \033[0m.
  4. You can combine everything:

    awk '/^(Username|Assigned IP)/{sub("^(Username|Assigned IP)", "\033[1;32m&\033[0m"); print} /^Group Policy/{sub("^Group Policy", "\033[1;31m&\033[0m"); print}' session.log
    

Upvotes: 1

Related Questions