-2

Im trying to have awk script that read 8 inputs file and each file contains 4 lines: File 1 contains:

file 1 line 1  
file 1 line 2    
file 1 line 3  
file 1 line 4  

and all others have the same ..

So the required output will be:

pe    co         test 1           test 2


1       3         file 1 line 1    file 2 line 1  
                  file 1 line 2    file 2 line 2  
                  file 1 line 3    file 2 line 3  
                  file 1 line 4    file 2 line 4  

2       6         file 3 line 1    file 4 line 1  
                  file 3 line 2    file 4 line 2  
                  file 3 line 3    file 4 line 3  
                  file 3 line 4    file 4 line 4  

3       9         file 5 line 1    file 6 line 1  
                  file 5 line 2    file 6 line 2  
                  file 5 line 3    file 6 line 3  
                  file 5 line 4    file 6 line 4  

here is example output file : http://postimg.org/image/e8u2pyxsr/fcb1921d/

any help how to produce such output ?

thanks

4

1 回答 1

2
#!/usr/bin/awk -f

BEGIN {
    format = "%-8s%-10s%-17s%s\n"
    printf(format, "pe", "co", " test 1", " test 2")
    printf("%s\n", "-------------------------------------------------------")
    for (i = 1; i < ARGC; i += 2) {
        j = i + 1; ++pe
        if ((getline a < ARGV[i]) > 0 && (getline b < ARGV[j]) > 0) {
            printf(format, pe, pe * 3, a, b)
            while ((getline a < ARGV[i]) > 0 && (getline b < ARGV[j]) > 0) {
                printf(format, "", "", a, b)
            }
        }
    }
    exit
}

运行

awk -f script.awk file1 file2 file3 file4 file5 file6 file7 file8

输出:

pe      co         test 1           test 2
-------------------------------------------------------
1       3         file 1 line 1    file 2 line 1
                  file 1 line 2    file 2 line 2
                  file 1 line 3    file 2 line 3
                  file 1 line 4    file 2 line 4
2       6         file 3 line 1    file 4 line 1
                  file 3 line 2    file 4 line 2
                  file 3 line 3    file 4 line 3
                  file 3 line 4    file 4 line 4
3       9         file 5 line 1    file 6 line 1
                  file 5 line 2    file 6 line 2
                  file 5 line 3    file 6 line 3
                  file 5 line 4    file 6 line 4
4       12        file 7 line 1    file 8 line 1
                  file 7 line 2    file 8 line 2
                  file 7 line 3    file 8 line 3
                  file 7 line 4    file 8 line 4

表格格式:

#!/usr/bin/awk -f

BEGIN {
    printf("%s\n", "---------------------------------------------------")
    format = "| %-2s |   %-5s|  %-15s|  %-15s|\n"
    printf(format, "pe", "co", "   test 1", "   test 2")
    printf("%s\n", "---------------------------------------------------")
    for (i = 1; i < ARGC; i += 2) {
        j = i + 1; ++pe
        if ((getline a < ARGV[i]) > 0 && (getline b < ARGV[j]) > 0) {
            printf(format, pe, pe * 3, a, b)
            while ((getline a < ARGV[i]) > 0 && (getline b < ARGV[j]) > 0) {
                printf(format, "", "", a, b)
            }
        }
        printf("%s\n", "---------------------------------------------------")
    }
    exit
}

输出:

---------------------------------------------------
| pe |   co   |     test 1      |     test 2      |
---------------------------------------------------
| 1  |   3    |  file 1 line 1  |  file 2 line 1  |
|    |        |  file 1 line 2  |  file 2 line 2  |
|    |        |  file 1 line 3  |  file 2 line 3  |
|    |        |  file 1 line 4  |  file 2 line 4  |
---------------------------------------------------
| 2  |   6    |  file 3 line 1  |  file 4 line 1  |
|    |        |  file 3 line 2  |  file 4 line 2  |
|    |        |  file 3 line 3  |  file 4 line 3  |
|    |        |  file 3 line 4  |  file 4 line 4  |
---------------------------------------------------
| 3  |   9    |  file 5 line 1  |  file 6 line 1  |
|    |        |  file 5 line 2  |  file 6 line 2  |
|    |        |  file 5 line 3  |  file 6 line 3  |
|    |        |  file 5 line 4  |  file 6 line 4  |
---------------------------------------------------
| 4  |   12   |  file 7 line 1  |  file 8 line 1  |
|    |        |  file 7 line 2  |  file 8 line 2  |
|    |        |  file 7 line 3  |  file 8 line 3  |
|    |        |  file 7 line 4  |  file 8 line 4  |
---------------------------------------------------
于 2013-09-18T13:05:35.963 回答