我试图让awk脚本读取8个输入文件,每个文件包含4行: 文件1包含:
file 1 line 1
file 1 line 2
file 1 line 3
file 1 line 4
和所有其他人都有相同的..
所以所需的输出将是:
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
这是示例输出文件:http://postimg.org/image/e8u2pyxsr/fcb1921d/
任何帮助如何产生这样的输出?
感谢
答案 0 :(得分: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 |
---------------------------------------------------