Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am a beginner in shell scripting. Just made 1 script with the help of others and it is working like we expected. However i want to organize it more.

  • Current Script

    { date '+%F %T'; "$ipath"/cli iedge list | awk 'BEGIN { print "Registration ID", "Ongoing Calls"} /Registration ID/ { id = $3; next } /Ongoing Calls/ { print id,$3 }' } >> "$ifile"

  • Output which is coming as below

    • Registration ID Ongoing Calls
      • Nitin_03 30
      • Nitin_01 0
  • I want the output should come like as below.

 -------------------------------------
|Registration ID   |  Ongoing Calls |
-------------------------------------
| Nitin_03         |     30         |
-------------------------------------
| Nitin_01         |      0         |
------------------------------------- 

Can anyone please help me on this.

  • Here is the change and its working fine only thing when i added another "character" it is giving fatal error.I am getting error when i added "maxCallsOut" as you can see below

    ipath=/usr/local/nextone/bin ifile=/root/Nick.csv "$ipath"/cli igrp list | awk ' BEGIN { print "------------------------------------"; printf "|%-17s|%-16s|%-15s|\n","Iedge Group", "maxCallsIn"
    "maxCallsOut" } /Iedge Group/ { id = $3; next } /maxCallsIn/ /maxCallsOut/{print "------------------------------------"; printf
    "|%-17s|%-16s|%-15s\n",id,$3 } END{ print "------------------------------------"; }'

------------------------------------
awk: cmd. line:3: fatal: not enough arguments to satisfy format string
        `|%-17s|%-16s|%-15s|
'
                         ^ ran out for this one
share|improve this question
    
You did not get that code from me. I did not, for example, use printf id,$3. The corresponding equation in my code is printf "|%-17s|%-16s|\n",id,$3 which is very different. Please check my answer again. –  John1024 Jun 3 at 0:24
    
@john1024 i am sorry you are correct, i was trying incorrect manner what you suggested that is why it was coming in invalid format. –  Nitin Kapoor Jun 3 at 0:26
    
printf "|%-17s|%-16s|%-15s\n",id,$3 The reason for the error is that you added a third format string but did not add a third variable. The number of format strings and the number of variables need to match. –  John1024 Jun 3 at 0:37

1 Answer 1

To get formatted output from awk, use printf:

$ "$ipath"/cli iedge list | awk 'BEGIN { print "------------------------------------"; printf "|%-17s|%-16s|\n","Registration ID", "Ongoing Calls"} /Registration ID/ { id = $3; next } /Ongoing Calls/ {print "------------------------------------"; printf "|%-17s|%-16s|\n",id,$3 } END{print "------------------------------------";}'
------------------------------------
|Registration ID  |Ongoing Calls   |
------------------------------------
|Nitin_03         |30              |
------------------------------------
|Nitin_01         |0               |
------------------------------------

If it is easier to read, the following is the same but spread over several lines in a script:

#!/bin/sh
"$ipath"/cli iedge list | awk '
BEGIN { print "------------------------------------";
        printf "|%-17s|%-16s|\n","Registration ID", "Ongoing Calls"
      }
/Registration ID/ { id = $3; next }
/Ongoing Calls/ {print "------------------------------------"; printf "|%-17s|%-16s|\n",id,$3 }
END{
    print "------------------------------------";
   }
'

The printf statements can be adjusted to get whatever format you prefer.

share|improve this answer
    
i tried printf it is not wrking and even it is giving very bad output.. I updated in question..you can see now –  Nitin Kapoor Jun 2 at 23:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.