0

I have a script called "main.ksh" which returns "output.txt" file and I am sending that file via mail.(list contains 50+ records, I just give 4 records for example)

mail output I am getting is:

DATE | FEED NAMEs | FILE NAMEs | JOB NAMEs | SCHEDULED_TIME| TIMESTAMP| SIZE(MB)| COUNT| STATUS |

Dec 17 INVEST_AI_FUNDS_FEED amlfunds_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.4248 4031 On_Time

Dec 17 INVEST_AI_SECURITIES_FEED amltxn_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.0015 9 On_Time

Dec 17 INVEST_AI_CONNECTED_PARTIES_FEED amlbene_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.0001 1 No_Records

I am implementing coloring for Delayed,On_Time and No_Records field and I wrote below script which gives me bottom output(output is correct but there is no space separated).

awk 'BEGIN {
print "<html>" \
"<body bgcolor=\"#333\" text=\"#f3f3f3\">" \
"<pre>"
}

NR == 1 { print $0 }

NR > 1 {
if      ($NF == "Delayed")     color="red"
else if ($NF == "On_time")     color="green"
else if ($NF == "No_records")  color="yellow"
else                           color="#003abc"

$NF="<span style=\"color:" color "\">" $NF "</span>"

print $0
}

END {
print "</pre>" \
"</body>" \
"</html>"
}
' output.txt > output.html

output with perfect coloring:

| DATE | FEED NAMEs | FILE NAMEs | JOB NAMEs | SCHEDULED_TIME| TIMESTAMP| SIZE(MB)| COUNT| STATUS |

Dec 17 INVEST_AI_FUNDS_FEED amlfunds_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai On_Time

Dec 17 INVEST_AI_SECURITIES_FEED amltxn_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai On_Time

Dec 17 INVEST_AI_CONNECTED_PARTIES_FEED amlbene_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai No_Records

There are 4 columns are skipped automatically. Could you please help me on this please ? Thanks a lot !

3
  • After some hours re-reading your post I realized that you want to keep the tabular look. And I think I know why your awk script deletes those multiple blanks between a field and the next one. This problem ocurrs when you modify any of the fields of the input line like, for instance, when your code changes the last field: $NF="<span style=\"color:" color "\">" $NF "</span>". In the man pages of awk you can read: Assigning a value to an existing field causes the whole record to be rebuilt when $0 is referenced. Commented Nov 22, 2016 at 12:05
  • whether print fixed-width columns into the <pre> tag using AWK's sprintf(), or printf(): awk '{ printf("%-20s%15s", $1, $2) }' <<< 'abc 123', or build a table. Not necessarily using the <table> tag. You can build a table using only <div>s and CSS. Commented Nov 22, 2016 at 12:11
  • 1
    I don't post an answer, because I don't like the input format. A filename may contain spaces, tabs, newlines, actually anything, except \0, and /. By default, AWK splits the lines into fields by spaces. You can pass another delimiter with -F option. It could be /, for instance. But how do you know that the rest of the fields won't contain a slash? I mean that the input format is far from perfect for this task. I'd suggest XML, or JSON, or any other kind of strict format. Commented Nov 22, 2016 at 12:23

1 Answer 1

0

When your code executes this

$NF="<span style=\"color:" color "\">" $NF "</span>"

print $0

the input line is rebuilt and therefore the multiple blanks between two consecutive fields are replaced by just ONE only blank space.

My solution copies the input line in a variable, deletes the last field (changing the value of the variable, not the input line), adds the modified last field and prints:

Dummy=$0
sub("[^ ]+$","",Dummy)   # removes last field
Dummy=Dummy "<span style=\"color:" color "\">" $NF "</span>"

print Dummy

Best regards

update: the last two code lines can be reduced in this way:

print Dummy "<span style=\"color:" color "\">" $NF "</span>"
Sign up to request clarification or add additional context in comments.

3 Comments

I used your command but I am getting output with duplication, I mean there are two lines for each and every row. Example Nov 22 d1_psar_balance_20161122.txt 07:18 0.116 167 On_Time Nov 22 d1_psar_balance_20161122.txt 07:18 0.116 167 On_Time Nov 22 d2_psar_balance_20161122.txt 16:25 0 0 No_Records Nov 22 d2_psar_balance_20161122.txt 16:25 0 0 No_Records
Hi Sir, Your code is working fine. But it is not working if the input file contents more columns (question is updated). Could you please help me on this please ?
My answer solved your initial question. If you have another question, please open a new one as you have done using another user. Why do you use different users?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.