Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input file is uncolored; I want the output file to be colored on criteria
# 1  
Old 02-19-2014
Input file is uncolored; I want the output file to be colored on criteria

Hello,

I have the following input file:
Code:
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:
Code:
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:

Code:
#/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

# 2  
Old 02-19-2014
In bash test expression, the regular expression may not support the anchor.
It can be
Code:
if [[ $line =~ "auditing account:" ]] ; then

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

If you want use the anchor, try grep, like this:
Code:
if [ `echo "$line"|grep -c "^auditing account:"` -ne 0 ] ; then

Hope can help u.~

Lucas
# 3  
Old 02-19-2014
How about with case:
Code:
#!/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"

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-19-2014
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...
Code:
#/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

# 5  
Old 02-19-2014
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.
# 6  
Old 02-23-2014
Hi.
Quote:
Originally Posted by Corona688
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.
However, I have used ansifilter to convert to a few different formats:
Code:
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

Previous Thread | Next Thread
Test Your Knowledge in Computers #315
Difficulty: Easy
To bypass US Munitions Export Laws, the creator of the PGP published all the source code in a binary format.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

creating separate output file for each input file in python

Experts, Need your help for this. Please support My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput) Input files File 1(1.1.1.1.csv) a,b,c 43,17104773,3 45,17104234,4 File 2(2.2.2.2.csv) a,b,c 43,17104773,1... (2 Replies)
Discussion started by: as7951
2 Replies

2. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

4. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

6. Shell Programming and Scripting

Check input file with different criteria

HI Input file.txt ABCDE1 JFHFJFJF3 10 ABCDE2 JFHFJFJF5 20 ABCDE3 JFHFJFJF5 30 ABCDE4 JFHFJFJF6 - ABCDE5 JFHFJFJF6 20 ABCDE6 JFHFJFJF6 90 ABCDE7 JFHFJFJF6 9 ABCDE8 JFHFJFJF6 I want to check third column if data missing or wrong data the echo massage and out from script. 1.... (8 Replies)
Discussion started by: pareshkp
8 Replies

7. UNIX for Dummies Questions & Answers

12. If an ‘88’ Record with BAI Code ‘902’ was found on input file and not written to Output file, re

This is my input file like this 03,105581,,015,+00000416418,,,901,+00000000148,,,922,+00000000354,,/ 49,+00000000000416920,00002/ 03,5313236,,015,+00231036992,,,045,+00231036992,,,901,+00000048428,,/ 88,100,+0000000000000,0000000,,400,+0000000000000,0000000,/ 88,902,+0000000079077,,/... (0 Replies)
Discussion started by: sgoud
0 Replies

8. Shell Programming and Scripting

split input data file and put into same output file

Hi All, I have two input file and need to generate a CSV file. The existing report just "GREP" the records with the Header and Tailer records with the count of records. Now i need to split the data into 25 records each in the same CSV file. id_file (Input file ) 227050994 232510151... (4 Replies)
Discussion started by: rasmith
4 Replies

9. Shell Programming and Scripting

AWK Script to convert input file(s) to output file

Hi All, I am hoping someone can help me with some scripting I need to complete using AWK. I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format. The Input file is:- aaaa bbbbb ccccc 1 xxxx aaa bbb aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Discussion started by: jason_v_brown
9 Replies

10. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

Featured Tech Videos