awk or sed: change the color of a column w/o screwing up column spacing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed: change the color of a column w/o screwing up column spacing
# 1  
Old 08-25-2012
awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately.

Here's some example output:
Code:
  Iface   RxMBytes  RxPackets  RxErrs  RxDrop  TxMBytes  TxPackets  TxErrs  TxDrop
  bond0   9         83830      0       0       12        62866      0       0
  eth0    1         18689      0       0       0         0          0       0
  eth1    0         0          0       0       0         0          0       0
  eth2    0         0          0       0       0         0          0       0
  eth3    8         65141      0       0       12        62866      0       0
  eth4    1         12949      0       0       0         375        0       0
  eth5    0         0          0       0       0         0          0       0
  virbr0  0         0          0       0       0         22         0       0

I'd like to colorize the output as well -- it's easy enough to change the color of the header line, but the other piece I can't figure out is: changing the color of the interface name, i.e., the first field of line 2+.

Now, in a perfect world I would have my awk script generate the color and columnize the output but I haven't tried to figure out columnizing in awk yet -- it doesn't look easy. Here's what I did to colorize the first line:
Code:
the_above_output | awk -vC0='\033[0;0m' -vC1='\033[1;35m' '
  { if (NR == 1) print C1 $0 C0; else print }'

This won't work for the additional lines (i.e. the ones where I just want to change the color of $1), because as soon as I start picking out fields, I'll lose the carefully-crafted whitespace separating said fields.

I think sed is what I need... I suspect with the right switches it should be able to
  • operate on NR >= 2
  • regex replace the first word of each record with <color>&<color>
Any ideas will be greatly appreciated.

PS: I have an application I'm working on that uses tons of awk "modules" to do this kind of parsing of output. Some of them also pipe their output to column for the final display as well. If you know of any good resources on awk columnizing, I'd love to see 'em. Here's another example:
Code:
ETHTOOL
  eth0  link=UP 1000Mb/s full (autoneg=Y)  drv bnx2 v2.1.11 / fw bc
  eth1  link=DOWN                          drv bnx2 v2.1.11 / fw bc
  eth2  link=DOWN                          drv e1000e v1.4.4-k / fw 5.11-2
  eth3  link=UP 1000Mb/s full (autoneg=Y)  drv e1000e v1.4.4-k / fw 5.11-2
  eth4  link=UP 1000Mb/s full (autoneg=Y)  drv e1000e v1.4.4-k / fw 5.11-2
  eth5  link=DOWN                          drv e1000e v1.4.4-k / fw 5.11-2

That's generated by 2 awk commands piped to column. And currently, after that I pipe it to another awk to change the color of each line -- orange if the link is down and green if it's up. I'm happy with that, but I might prefer to change just the link=DOWN or link=UP part -- but again, I don't see how to do it without screwing up the spacing.

---------- Post updated at 06:20 AM ---------- Previous update was at 02:54 AM ----------

I spent a few hours working on other things and then it just hit me -- I could use awk's gensub() for this. I'd still love to learn more about awk columnizing, but ....
Code:
 the_above_proc/net/dev_output |
  awk  -vC0='\033[0;0m' -vC1='\033[1;35m' -vC2='\033[0;34m' '
    {
      if (NR == 1) print C1 $0 C0
      else printf gensub(/(^  [[:graph:]]+ )/, C2"\\1"C0, 1)"\n"
    }'

# 2  
Old 08-26-2012
You could also use sed to insert a column separator (comma in this case) and then use sort of your first awk cmd:
Code:
sed 's/  */&,/g' test| awk -vC0='\033[0;0m' -vC1='\033[1;35m' '
  { if (NR == 1) {$1=$1;$0=C1$0C0} else $1=C1$1C0; print }' FS=","

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-26-2012
You can do it all in one awk. You know the size of your fixed-width columns, you use printf(). example

Code:
printf("%s%-10s%s ...", C1, intf, C0, ...)

or I guess hardcode the colors into the format string.. ideally you'd use tput to extract the proper information for the terminal rather though.
# 4  
Old 08-26-2012
I don't think the col width is known in advance, in above example it ranges from 6 to 8, and even the header would not be a reliable indicator as you see in the Iface col. column does a fine job here, and it is not that easy to keep the col alignment intact.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

5. Shell Programming and Scripting

Change file content 4 column to one Column using script

Hi Gurus, I have file content sample: ,5113955056,,TAgent-Suspend ,5119418233,,TAgent-Suspend ,5102119078,,TAgent-Suspend filenames 120229H5_suspend, 120229H6_unsuspend I receive those files one of directory /home/temp/ I need following: 1. Backup first /home/temp/ file to... (5 Replies)
Discussion started by: thepurple
5 Replies

6. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

7. Ubuntu

Inserting a header with column number to a 1.6 GB file with special spacing

Hi; I've been searching posts to find a solution to what I'm trying to do, but I've have NOT found anything yet. I have a file (file1) with 300K columns and 1411 rows, the columns don't have a column no. header (No header at all) and I'm trying to fetch the information from specific columns.... (3 Replies)
Discussion started by: sogi
3 Replies

8. Shell Programming and Scripting

Change names in a column based on the symbols in another column

If the 4th column has - sign then the names in 3rd column has to change to some user defined names (as shown in output). Thanx input1 1 a aaaaa + 2 b bbbbb + 3 c ccccc + 4 d ddddd + 5 e eeeee + 6 f xxxxx + 8 h hhhhh +... (8 Replies)
Discussion started by: repinementer
8 Replies

9. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

10. UNIX for Dummies Questions & Answers

Change one column using sed??

Is there way to use sed to change only one occurence in a colon separated line? I would like to change a file from *:*:rex:rex *:*:simon:rex to *:*:rex:mabry *:*:simon:rex (2 Replies)
Discussion started by: rexmabry
2 Replies
Login or Register to Ask a Question