Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a table using awk

Status
Not open for further replies.

lotamoka

MIS
Jul 15, 2007
43
US
I have following files ...
$ cat drive17_status
Drive17
======
Jul 11 tsm03 BW5450
Jul 11 tsm03 BW4538
$ cat drive7_status
Drive7
======
Jul 13 tsm03 BR3364
Jul 13 tsm05 BR1989
$ cat drive6_status
Drive6
======
Jul 13 tsm01 BR3363
Jul 13 tsm05 BR1990
$cat drive8_status
$
And output should look like ...
$cat drive_status
Drive6 Drive7 Drive17 Drive8_status
====== ======== ======== ======
Jul 13 tsm01 BR3363 Jul 13 tsm03 BR3364 Jul 11 tsm03 BW5450
Jul 13 tsm05 BR1990 Jul 13 tsm05 BR1989 Jul 11 tsm03 BW4538
Any help appreciated ?
 
can you try the paste command see if that meets your objective
Code:
#paste drive17_status drive7_status drive6_status drive8_status > output.txt
#cat output.txt
There may be an elegant awk solution too as you asked in an awk forum
 
i used paste but table gets meshed up. Is there way i can use printf which will put each column with fixed length ?
 
Something like this would do it, although it won't handle the empty drive8_status file the way you wanted:

Code:
awk '
        { line[FNR]=sprintf("%s%-30s",line[FNR], $0) }
        FNR > max { max=FNR }
        END { for (i=1; i<=max; i++) { print line[i] } }
' drive*_status

It might be easier to handle that particular scenario when you actually generate the source files.

Annihilannic.
 
I have numerous file like above but some of file will be empty. I want each files out put in fixed colummn no mater if they empty or has some thing. Output file should have column belong to each file and their output limited to their column.
Having hard time to get this output in tabular form.
Thank you very much.
 
Well, it was difficult to modify the above solution to do that as awk doesn't test any expressions against empty input files.

This method processes the arguments manually instead in the BEGIN {} clause:

Code:
awk '
    BEGIN {
        lines=5
        # for each filename argument
        for (f=1; f<ARGC; f++) {
            # for each line of report output
            for (l=1; l<lines; l++) {
                if (getline < ARGV[f]) {
                    line[l]=sprintf("%s%-25s",line[l],$0)
                } else {
                    if (l==1) {
                        # first line empty, print filename
                        line[l]=sprintf("%s%-25s",line[l],ARGV[f])
                    } else {
                        # add empty column
                        line[l]=sprintf("%s%-25s",line[l]," ")
                    }
                }
            }
            close(ARGV[f])
        }
        # print the report
        for (i=1; i<=lines; i++) { print line[i] }
    }
' drive*_status

Annihilannic.
 
Thank you Annihilannic , you solved my problem.
By the way I am not able to understand above awk. Would you be more explanatory ?
Thanx again.
 
I thought my comments in the code were quite explanatory; which parts do you need me to clarify?

One thing you may not be aware of is that ARGC is the count of arguments (parameters) supplied to awk on the command-line, and ARGV[] is an array containing those arguments.

Annihilannic.
 
I am very bad when comes ARRAY. I put ?? mark where i could not get.


awk '
BEGIN {
??lines=5
# for each filename argument
for (f=1; f<ARGC; f++) {
# for each line of report output
for (l=1; l<lines; l++) {
if (getline < ??ARGV[f]?) {
?? line[l]=sprintf("%s%-25s",line[l],$0)
} else {
if (??l==1) {
# first line empty, print filename
line[l]=sprintf("%s%-25s",line[l],ARGV[f])
} else {
# add empty column
line[l]=sprintf("%s%-25s",line[l]," ")
}
}
}
??close(ARGV[f])
}
# print the report
for (i=1; ??i<=lines; i++) { print line }
}
' drive*_status
 
The "lines" variable just contains the number of lines of report output to create.

You need to read about arrays to understand most of the rest of the code, there is plenty of information out there. Basically an array is a variable containing multiple values indexed by another value. e.g. you could use this one to convert digits to their textual names:

Code:
awk 'BEGIN {
    a[1]="one"
    a[2]="two"
    a[3]="three"

    v=2
    print "the name of " v " is " a[v]
}'

close(ARGV[f) is just used to close the file previously opened using getline < ARGV[f].

Annihilannic.
 
Hi
I came accross one critical juncture, some of file has more than 4 records in it.
$ cat drive9_status
Drive9
======
Jul 13 tsm01 BR3363
Jul 13 tsm05 BR1990
Jul 15 tsm04 BR1254
Jul 17 tsm01 BR1248

Is there way can you modifiy ur awk to accomodate any file having any number of records ?

Thank you very much.
 
What have YOU tried so far and where in YOUR code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
lines=5 ..Is there way to calculate max number of lines in any files then set lines=NewMAX ?
Since some files has 10 lines other may have 100 or more.
THX
 
Try using the wc and sort commands on all of the input files first to find out which one has the most lines. Then use this value to set the lines variable in the awk script using the awk -v lines=value command-line option.

Annihilannic.
 
Hi
Annihilannic (MIS)
I have above code hardcoded in a script which runs daily. Number of records varies daily in each file. Is there way u can modify ur awk to accomodate future changes on fly then set maxvalue for LINES variable ?

THX
 
You can do what I described above automatically in your script. Have you tried it?

Annihilannic.
 
Hi
Question is .....above awk has input only from files drive*_status . How do i supply lines variable to awk ? Is there way i can input drive*_status and a variable to above wk ?
 
Yes, have a look at the -v option on the awk man page for details.

e.g.

Code:
LINES=(whatever you calculate)
awk -v lines=$LINES '
   <awk script here>'
' inputfile > outputfile

Annihilannic.
 
Hi
Annihilannic
Now my script is complete. Thank you for ur help.
This forum is wornderfull
Thx again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top