Input file is uncolored; I want the output file to be colored on criteria

Hello,

I have the following input file:

auditing account: 3DTP (3dtp)
ERROR: S3 bucket "aws-origin-test1.3dstage.com" has policy statement with public grant: {"Sid":"PublicReadGetObject","Effect":"Allow","Principal":{"AWS":"*"},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::aws-origin-test1.3dstage.com/*"]}
auditing region: eu-west-1
auditing region: sa-east-1
auditing region: us-east-1
WARNING: instances have more than 100 security group rules: i-cf67f5b7 i-520a8436 i-1ac9477c i-42c94724 i-174ed56f i-53cfad29 i-f62d4290 i-94bf4de8 i-664ee601
ERROR: DB security group "3dstg" violates RDS policy: allows x.x.x.x/32
ERROR: DB instance "aws" is a member of DB security group "3dstg"

I want my output file to be colored based on following criteria:
If a line starts with "auditing account:" then the line should be in 'BLUE' font
If a line starts with "WARNING:" then the line should be in 'ORANGE' font
If a line starts with "ERROR:" then the line should be in 'RED' font
All other lines , the line should be in 'BLACK' font

In other words; this is how my output file should look like:

auditing account: 3DTP (3dtp)
ERROR: S3 bucket "aws-origin-test1.3dstage.com" has policy statement with public grant: {"Sid":"PublicReadGetObject","Effect":"Allow","Principal":{"AWS":"*"},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::aws-origin-test1.3dstage.com/*"]}
auditing region: eu-west-1
auditing region: sa-east-1
auditing region: us-east-1
WARNING: instances have more than 100 security group rules: i-cf67f5b7 i-520a8436 i-1ac9477c i-42c94724 i-174ed56f i-53cfad29 i-f62d4290 i-94bf4de8 i-664ee601
ERROR: DB security group "3dstg" violates RDS policy: allows x.x.x.x/32
ERROR: DB instance "aws" is a member of DB security group "3dstg"

PS: My color codes are exactly not what i want in the script; I can change them; all I need is a regular expression to match what the line starts with.
I need a small script that takes care of this:

#/bin/bash

IPfile=file.txt
OPfile-file1.txt
while read line in $IPfile
do
if [[ $line "^auditing account:" ]] ; then
echo -e "\031[0;31m'$line'\031[m \031[0;36m${0}\031[m \031[0;32m >>OPfile
elif [[ $line "^ERROR:" ]] ; then
echo -e "\033[0;31m'$line'\033[m \033[0;36m${0}\033[m \033[0;32m >>OPfile
elif [[ $line "^WARNING:" ]] ; then
echo -e "\028[0;31m'$line'\028[m \028[0;36m${0}\028[m \028[0;32m >>OPfile
else 
echo -e "\029[0;31m'$line'\029[m \029[0;36m${0}\029[m \029[0;32m >>OPfile
fi
done

In bash test expression, the regular expression may not support the anchor.
It can be

if [[ $line =~ "auditing account:" ]] ; then

but not

if [[ $line =~ "^auditing account:" ]] ; then

If you want use the anchor, try grep, like this:

if [ `echo "$line"|grep -c "^auditing account:"` -ne 0 ] ; then

Hope can help u.~

Lucas

How about with case:

#!/bin/bash

BLUE="\e[0;34m"
ORANGE="\e[1;33m"
RED="\e[0;31m"
NOCOLOR="\e[0m"
IPfile=file.txt
OPfile=file_out.txt
while read -r line 
do
  case $line in
    "auditing account:"*) color=$BLUE    ;;
    ERROR:*)              color=$RED     ;;
    WARNING:*)            color=$ORANGE  ;;
    *)                    color=$NOCOLOR ;;
  esac
  printf "$color%s$NOCOLOR\n" "$line" 
done < "$IPfile" >"$OPfile"
1 Like

There isn't any orange ANSI escape code so I used magenta. You seem to be displaying the script name in cyan. I left that in. So this seems to work...

#/bin/bash

IPfile=file.txt
OPfile=file1.txt
exec < $IPfile > $OPfile
while read line
do
        if [[ $line = "auditing account:"* ]] ; then
                echo -e "\\033[0;34m${line}\\033[m \\033[0;36m${0}\\033[0;39m"
        elif [[ $line = "ERROR:"* ]] ; then
                echo -e "\\033[0;31m${line}\\033[m \\033[0;36m${0}\\033[0;39m"
        elif [[ $line  = "WARNING:"* ]] ; then
                echo -e "\\033[0;35m${line}\\033[m \\033[0;36m${0}\\033[0;39m"
        else
                echo -e "\\033[0;31m${line}\\033[m \\033[0;36m${0}\\033[0;39m"
        fi
done
exit 0

The colors will only show when printed to terminal of course. Open in a text editor, or attach it to an email, and it'll be garbage.

Hi.

However, I have used ansifilter to convert to a few different formats:

Ansifilter - ANSI escape code processor and converter

See Andr� Simon - Startseite for details. It compiled easily with g++ (Debian 4.3.2-1.1) 4.3.2 with the included makefile ... cheers, drl