Simplest way to format with If on stout?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simplest way to format with If on stout?
# 1  
Old 09-14-2011
Simplest way to format with If on stout?

So I'm trying to figure out a way to do some very simple formatting on standard output. I have a command that I will run (many many times) the output will either be true or false. So all i really want is to run the command and if its true write true in green and if its false to write false in red. Ideally I don't want to be writing anything or reading anything from files. Simply run command and take the output and print it. Nothing permanent saved.

I can use awk with print to simply take standard output and format it how I want. I can't seem to get my if/else working with awk though. Is there some other way I can simply pipe standard output to a true / false logic that might be simpler than awk?

---------- Post updated at 12:11 PM ---------- Previous update was at 11:39 AM ----------

Code:
command | awk '{if($0 eq true) print "\033[1;32m" $0; else print "\033[1;31m" $0}'

This is what i'm doing now... Seems like the simplest possible way I can do it and it kinda works. It prints the output but my if doesn't seem to be working.. no matter what $0 is I get the same color output. Smilie
# 2  
Old 09-14-2011
true is an integer, 1. "true" is a string.

So command | awk '{if($0 == "true") print "\033[1;32m" $0; else print "\033[1;31m" $0}'
# 3  
Old 09-14-2011
Hate to beat a dead horse, but with regards terminal capabilities, it's still "better" to not hardcode terminal specifics but to query and use terminal capabilities (e.g. from terminfo database).

So, better might be:

Code:
green=`tput setaf 2`
red=`tput setaf 1`
norm=`tput sgr0`

command | awk 'BEGIN {red="'"$red"'";green="'"$green"'";norm="'"$norm"'";}{if($0 == "true") print green $0 norm; else print red $0 norm}'

and with that said, realize that the color table isn't guaranteed on a terminal... and changing it can be easy to difficult... and in some cases if you do change it, you'll want to change it back to whatever it was originally... again, you hope the terminal capabilities are there for all of that.... YMMV.
# 4  
Old 09-14-2011
Quote:
Originally Posted by cjcox
Hate to beat a dead horse, but with regards terminal capabilities, it's still "better" to not hardcode terminal specifics but to query and use terminal capabilities (e.g. from terminfo database).
As long as all he's doing is colors it ought to be just fine.

---------- Post updated at 02:31 PM ---------- Previous update was at 02:27 PM ----------

You should use -v for setting outside variables in awk.

Code:
echo false | awk -v R="`tput setaf 1`" -v G="`tput setaf 2`" -v N="`tput sgr0`" '{if($0 == "true") print G $0 norm; else print R $0 norm}'

# 5  
Old 09-15-2011
The -v option is not uniformly supported.. but certainly, if it's there, use it (if you don't mind sacrificing some portability).
# 6  
Old 09-15-2011
Quote:
Originally Posted by cjcox
The -v option is not uniformly supported..
If your system has an awk that bad, it probably has a nawk you should be using instead...
# 7  
Old 09-15-2011
Appreciate all the answers. So far I have made so much progress on my custom prompt and i'm quite happy with the results. It's so streamline and customized that it answers about 20 different questions about the system at a glance. The last thing i'm trying to do involves the same process but I need to be able to compare sdout with a variable in side of awk. I can't seem to get it to work with a variable. The output of the command will be the IP address the hostname resolves to and the variable $actualip is the actual configured IP on the system. So the idea is that they will match when properly configured.

Code:
command | awk '{if($0==$actualip) print "\033[5;33m" $0 "\033[0;35m\" ;  else print "\033[0;32m" $0}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Simplest way to access Linux system mail from email client

I'm not a Linux newbie but this seemed like a pretty n00b question. I am familiar with Postfix/Dovecot setups but now I am looking for the simplest way to access mail on a Linux system, such as the mail that goes to /var/mail/root. I know I can edit /etc/aliases and have the system mail go straight... (6 Replies)
Discussion started by: TayKimchi
6 Replies

2. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

3. Programming

The simplest network driver

Hi, I am trying to write the simplest network driver that would send whatever through cable. My configuration is: Linux machine with some Intel network adapter Another machine with WireShark I connected Intel network adapter to second machine and want anything to pop up at wireshark. ... (12 Replies)
Discussion started by: Chrisdot
12 Replies

4. UNIX for Dummies Questions & Answers

Should be the simplest thing in the world grep

should be the simplest thing in the world use grep but i not get it i have several files, in this files i need to khow how many times the words are repeated but the files must star whit P_ this work egrep -c "word1|word2" *P_* result: P_11351814:1 P_11351823:3 P_11351826:1... (2 Replies)
Discussion started by: SirGrifo
2 Replies

5. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

6. Shell Programming and Scripting

stout, stderr to syslog via function with if statement

I have a function called sysLogger in a bash script that I am using to redirect stdout and stderr to syslog. Part of the script contains an option to turn on debugging otherwise I want debugging output to go to /dev/null. I am struggling to understand how to make this work using the function... (10 Replies)
Discussion started by: jelloir
10 Replies

7. Shell Programming and Scripting

Retaining the Unix CSV format in Excel format while exporting

Hi All, I have created a Unix Shell script whch creates a *.csv file and export it to Excel. The problem i am facing is that Users wants one of the AMOUNT field in comma separted values. Example : if the Amount has the value as 3000000 User wants to be in 3,000,000 format. This Amount format... (2 Replies)
Discussion started by: rawat_me01
2 Replies

8. UNIX for Dummies Questions & Answers

How can I find files by date or size from stout?

Hello all I wander if I make for example " ls -l " And it gives me all the files in the directory with the additional info like data size and privileges But what if I like to filter the stout result for example by date When I try to do: echo "`ls -l`" | grep "Jan 12" it gives me the... (2 Replies)
Discussion started by: umen
2 Replies

9. UNIX for Dummies Questions & Answers

I'm incapable of the simplest thing

Okay, stupid question How can I use ed, exactly? I need just a quick tutorial, thanks. Cheers! Tavy (2 Replies)
Discussion started by: Tavy
2 Replies

10. UNIX for Dummies Questions & Answers

the simplest question in the WORLD

how do i get unix on my windows 95 computer? (3 Replies)
Discussion started by: killfry
3 Replies
Login or Register to Ask a Question