awk print formatting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk print formatting
# 1  
Old 05-07-2013
awk print formatting

Hi all,

Is it possible to tell awk that the first line of text is a header and then print dashes after it is printed before it prints the rest of the text?

Note example below:

Source file:

Code:
Filesystem            kbytes    used   avail capacity  Mounted on
server-p01:/vol/vol_vol01/db01 8388608 7686744 701864 92% /nas_mnt/voL_vol01/db01
server-p01:/vol/vol_vol01/db02 23068672 21426148 1642524 93% /nas_mnt/vol_vol01/db02

Output required

Code:
Filesystem                            kbytes        used      avail   capacity   Mounted on
--------------------------------   ---------    --------    -------  ---------   ------------------------
server-p01:/vol/vol_vol01/db01       8388608     7686744     701864        92%   /nas_mnt/voL_vol01/db01
server-p01:/vol/vol_vol01/db02      23068672    21426148    1642524        93%   /nas_mnt/vol_vol01/db02

- Currently am doing roughly as below

Code:
echo "--------------------------------   ---------    --------    -------  ---------   ------------------------" > tmp.hed
df -k > tmp.00
num_lines=`wc -l tmp.00 | awk '{ print $1 }'`
let num_lines=${num_lines}-1
head -1 tmp.00 > tmp.02
tail -${num_lines} tmp.00 > tmp.03

# - then using awk and printf
awk printf tmp.02 # - still playing around with the modifiers
cat tmp.hed
awk printf tmp.03 # - still playing around with the modifiers

While I am getting the output that I wanted, just want to know maybe there is a smarter way of doing it.

Wish I can tell awk to print repeating characters
# 2  
Old 05-07-2013
Do you want just the dashed line after header or you want to format the file as well?
# 3  
Old 05-07-2013
Here is an awk code that seems to work:
Code:
awk '   BEGIN {
                n = split( "Filesystem-kbytes-used-avail-capacity-Mounted on", H, "-" )
                for ( i = 1; i <= n; i++ )
                        M[i] = M[i] < length (H[i]) ? length (H[i]) : M[i]
        }
        NR > 1 {
                for ( i = 1; i <= NF; i++ )
                {
                        A[$1]
                        V[$1,i] = $i
                        M[i] = M[i] < length ($i) ? length ($i) : M[i]
                }
        }
        END {
                for ( i = 1; i <= n; i++ )
                {
                        fmt = "%" M[i] "s "
                        printf fmt, H[i]
                }
                printf "\n"
                for ( i = 1; i <= n; i++ )
                {
                        while ( ++j <= M[i] )
                                printf "-"
                        printf " "
                        j = 0
                }
                printf "\n"
                for ( k in A )
                {
                        for ( i = 1; i <= NF; i++ )
                        {
                                fmt = M[i] "s "
                                printf "%" fmt,  V[k,i]
                        }
                        printf "\n"
                }
        }
' tmp.00
                    Filesystem   kbytes     used   avail capacity              Mounted on
------------------------------ -------- -------- ------- -------- -----------------------
server-p01:/vol/vol_vol01/db01  8388608  7686744  701864      92% /nas_mnt/voL_vol01/db01
server-p01:/vol/vol_vol01/db02 23068672 21426148 1642524      93% /nas_mnt/vol_vol01/db02

# 4  
Old 05-07-2013
Quick and dirty:
Code:
df -k | awk '
{print}
NR==1 {
print "--------------------------------   ---------    --------    -------  ---------   ------------------------"
}'

A bit more sophisticated (needs nawk or Posix awk or GNU awk):
Code:
df -k | awk '{print} NR==1 {gsub("[^ ]","-"); print}'

or with sed
Code:
df -k | sed '1p;1s/[^ ]/-/g'


Last edited by MadeInGermany; 05-07-2013 at 05:58 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File formatting with awk

Hi, I would like to format the file input example to the specific output example. I have tried numerous different ways, however not able to extract the information as desired. Any assistance to get the file formatted would be truly appreciated: Input: server:<server... (3 Replies)
Discussion started by: omuhans123
3 Replies

2. Shell Programming and Scripting

Suffix formatting with awk

i would like to format the 9 character with suffix as "0". i tried below it doesn't work. >a=12345 > echo $a | awk '{printf "%-09s\n",$1}' >12345 required output is 123450000 can you guys help me out ? (7 Replies)
Discussion started by: expert
7 Replies

3. Shell Programming and Scripting

awk : finding and formatting

Hi , I was trying to find files of a particular date and did that but then I also wanted to format a field based on some condition so had put another if else in awk. Now it is getting the files of particular date or also the files which are matching that if else condition. find . -name "*"... (1 Reply)
Discussion started by: abhi1988sri
1 Replies

4. UNIX for Dummies Questions & Answers

Help with awk'ing formatting this

Hi, I have a file below that I am wanting to awk. The lines of relevance are lines 7 and 9 $ nl /tmp/x 1 ADRCI: Release 11.2.0.3.0 - Production on Sun Jun 23 17:01:02 2013 2 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. 3 ADR base =... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. UNIX for Dummies Questions & Answers

awk formatting

Hi all, I'm writing a simple awk code: awk 'BEGIN {FS="|"};{print "Type\tNumber\ttypes\tTotal";};{print $1, "\t", $2, "\t", $3, "\t", $4, "\t";}' db_query.txt it gives me the result: Type Number types Total XXX 498.0 5100.0 5274.661 Type Number types Total... (7 Replies)
Discussion started by: messi777
7 Replies

6. Shell Programming and Scripting

formatting awk

when i try this awk its giving out put as below. awk '!(/^$/||/--/||/selected/||/^ *$/){print $1}' tmp.txt output ===== 1 2010-08-03-12.31.26.126000 how excluede the 1st line ? i mean i want output only 2nd line i.e 2010-08-03-12.31.26.126000; (5 Replies)
Discussion started by: rocking77
5 Replies

7. Shell Programming and Scripting

AWK formatting help.

Dear all I require help with AWK regarding this situation Input is : fn1 12345 fn1 23456 fn3 231513 fn1 22325 fn3 123125 Desired output is fn1 12345 23456 22325 fn3 231513 123125 (5 Replies)
Discussion started by: Peasant
5 Replies

8. Shell Programming and Scripting

String formatting using AWK

Hi, I need to insert a line at a particular line number. I am using the below code: sed $REV_LINO_NO" i\\ # $CURRENT_DATE $NAME Changed pwd for cindy\'s id" file > file1 This code works, but the formatting is not as I expected. For example, I get lines as shown below... (2 Replies)
Discussion started by: sugan
2 Replies

9. Shell Programming and Scripting

AWK Formatting Problem

Hi All, I'm having a problem with the way awk is interperting a space between double quotes in a for loop. Below is the code and output from running the script: AWK for loop: for i in $(awk 'BEGIN{FS=","}{print "Probe Name:" $1};{print "Probe Temp:" $2};{ print... (2 Replies)
Discussion started by: cstovall
2 Replies

10. Shell Programming and Scripting

Formatting using awk

Let's say I write a simple script that contains the following: date | awk '{print $1}' date | awk '{print $2}' Of course, when I run the script the output will look similar to: Tue Mar What if I want my ouput to be on one line as follows: Tue Mar What changes would I need to... (2 Replies)
Discussion started by: cdunavent
2 Replies
Login or Register to Ask a Question