How to print and append output of nawk script in commandline and as well into a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print and append output of nawk script in commandline and as well into a file?
# 1  
Old 03-05-2013
How to print and append output of nawk script in commandline and as well into a file?

Hi All,
I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file.

Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file.

script :
Code:
nawk 'BEGIN    {CmpAr["VerifyInterface"]=2         # some definitions
               CmpAr["CoreFileGenerated"]=3
               CmpAr["VerifyRegister"]=4
               CmpAr["VerifyComponent"]=5
               CmpAr["VerifyServiceAvailability"]=6
               split ("%20s %30s %22s %29s %26s %35s", FMT, " ")
               red="\033[1;31m"
               grn="\033[1;32m"
               rst="\033[0m"
               clr["Failed"]=red
               clr["Passed"]=grn
              }
     #function prt(){printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst; printf " |\n" }
function prt(){
        printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst;printf " |\n >> $AUTOMAT_DEST/${date_file}_Daily_Check_result"
}
#NR == FNR     {print; next}                  # read & print header
     #($2 in CmpAr) {gsub (" ", "", $1);
      ($2 in CmpAr) {gsub (/[ \t]/,"",$1);
                    OutAr[CmpAr[$2]]=$1;          # put $1 into the right column
                    OutAr[1]=$3}                  # save server name
     FILENAME != oldfn {                          # print if old file ends
                   if (oldfn) prt(); oldfn = FILENAME}
     END              {prt()}                     # print when last file ends
  ' FS=":"   /tmp/*_Daily_Check.log

function :
Code:
function prt(){
        printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst;printf " |\n >> $AUTOMAT_DEST/${date_file}_Daily__result"
}

I get the result in the console/screen with full path printed instead i was expecting output to be append to that file.


output :
Code:
|========================================================================================================|
|                               CONSOLIDATED REPORT STATUS                                               |
|========================================================================================================|
|Server                     Interface Checks                    Process Checks              Service Check|
|                       VerifyInterface  Corefile       VerifyRegister VerifyComponent                   |
|========================================================================================================|
| U01A             Passed     Passed            Passed         Passed                  Passed            |
 >> /tmp/030513_10H47M55S_Daily_Check_result| UD01B             Passed     Failed            Passed         Passed                  Passed |
 >> /tmp/030513_10H47M55S_Daily_Check_result|
|========================================================================================================|

# 2  
Old 03-05-2013
Use Tee command to send output to console and file

Code:
nawk 'BEGIN    {CmpAr["VerifyInterface"]=2         # some definitions
               CmpAr["CoreFileGenerated"]=3
               CmpAr["VerifyRegister"]=4
               CmpAr["VerifyComponent"]=5
               CmpAr["VerifyServiceAvailability"]=6
               split ("%20s %30s %22s %29s %26s %35s", FMT, " ")
               red="\033[1;31m"
               grn="\033[1;32m"
               rst="\033[0m"
               clr["Failed"]=red
               clr["Passed"]=grn
              }
     #function prt(){printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst; printf " |\n" }
function prt(){
        printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst;printf " |\n "}
#NR == FNR     {print; next}                  # read & print header
     #($2 in CmpAr) {gsub (" ", "", $1);
      ($2 in CmpAr) {gsub (/[ \t]/,"",$1);
                    OutAr[CmpAr[$2]]=$1;          # put $1 into the right column
                    OutAr[1]=$3}                  # save server name
     FILENAME != oldfn {                          # print if old file ends
                   if (oldfn) prt(); oldfn = FILENAME}
     END              {prt()}                     # print when last file ends
  ' FS=":"   /tmp/*_Daily_Check.log  | tee -a $AUTOMAT_DEST/${date_file}_Daily_Check_result

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 03-05-2013
Quote:
Originally Posted by Optimus81
Code:
function prt(){
        printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst;printf " |\n >> $AUTOMAT_DEST/${date_file}_Daily__result"
}

Please check those quotes. You have included path also in the quotes that's why it is printing whole path. Please change those quote location and try..

It should be like

Code:
function prt(){
        printf"|";for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst;printf " |\n" >> $AUTOMAT_DEST/${date_file}_Daily__result
}

Regards,

pamu
# 4  
Old 03-05-2013
Thanks Anbu23. Your suggested solution does the trick for me.

Thanks to pamu for prompt response.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

3. Shell Programming and Scripting

Append column using awk/nawk

Is there any way I can achieve this? Considering test1 : a 1 2 3 4 b 2 3 4 5 c 12 1232 14 1 d 10 13 4 5 e 1 5 6 9 1 And test to be some string : qw twe tew we qw I want something like this : a 1 2 qw 4 b 2 3 twe 5 (5 Replies)
Discussion started by: aksijain
5 Replies

4. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

5. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

6. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

7. Shell Programming and Scripting

How to append the output continously from a script

Hi All, Am using the below script to produce some statistics. Currently it send the results to a log file and sends the contents of the log to a mail ID. Next time when it runs it erases the previous log and writes the latest output to the log file. I want the output to be appended to... (2 Replies)
Discussion started by: nirmal84
2 Replies

8. UNIX for Dummies Questions & Answers

how to print script output to screen and file

Hi all, I have a script that bulk loads thousands of lines of data. I need to log the output during the execution of the script. I know I can redirect (">") the output to a file; however, I want the output going to both the screen and the log file. I thought I could use pipe to pipe the... (10 Replies)
Discussion started by: orahi001
10 Replies

9. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

10. Shell Programming and Scripting

append field to file(nawk)

I have two files. File A and File B. File A has two fields separated by comma 352020252365988, 652020100572356 546876543215667, 652065465654686 ... File B has many Fields separate by spaces Date Name 352020252365988 Reference Date2 Name2 546876543215667 Reference I want to... (4 Replies)
Discussion started by: axl
4 Replies
Login or Register to Ask a Question