awk 操作多个文件
awk to manipulate multiple files
我有以下文件 file1 和 file2(例如)
$cat file1.txt
SERVICE: 1
TASK: 1
RESULT: 1
ADDITIONAL: 1
SERVICE: 2
TASK: 2
RESULT: 2
ADDITIONAL: 2
SERVICE: 3
TASK: 3
RESULT: 3
ADDITIONAL: 3
$cat file2.txt
SERVICE: 1
TASK: 1
RESULT: 1
ADDITIONAL: 1
SERVICE: 2
TASK: 2
RESULT: 2
ADDITIONAL: 2
SERVICE: 3
TASK: 3
RESULT: 3
ADDITIONAL: 3
我需要以下输出:
文件名和服务上循环的每个块:在下面的 1 行中,用竖线“|”分隔“
file1.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file1.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file1.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
file2.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file2.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file2.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
你能帮我想办法实现这个吗
到目前为止,我能够将它排成一行,但我无法在服务上循环的每个块前面获取文件名:
我的命令如下:
cat * | awk -F ":" '/^SERVICE:/ {print ""} {printf"|" substr([=13=], index([=13=],))}'
非常感谢您的帮助
$ awk -v RS= -F'\n' -v OFS=' | ' '{ = FILENAME OFS }1' file1.txt file2.txt
file1.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file1.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file1.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
file2.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file2.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file2.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
我有以下文件 file1 和 file2(例如)
$cat file1.txt
SERVICE: 1
TASK: 1
RESULT: 1
ADDITIONAL: 1
SERVICE: 2
TASK: 2
RESULT: 2
ADDITIONAL: 2
SERVICE: 3
TASK: 3
RESULT: 3
ADDITIONAL: 3
$cat file2.txt
SERVICE: 1
TASK: 1
RESULT: 1
ADDITIONAL: 1
SERVICE: 2
TASK: 2
RESULT: 2
ADDITIONAL: 2
SERVICE: 3
TASK: 3
RESULT: 3
ADDITIONAL: 3
我需要以下输出:
文件名和服务上循环的每个块:在下面的 1 行中,用竖线“|”分隔“
file1.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file1.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file1.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
file2.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file2.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file2.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
你能帮我想办法实现这个吗
到目前为止,我能够将它排成一行,但我无法在服务上循环的每个块前面获取文件名:
我的命令如下:
cat * | awk -F ":" '/^SERVICE:/ {print ""} {printf"|" substr([=13=], index([=13=],))}'
非常感谢您的帮助
$ awk -v RS= -F'\n' -v OFS=' | ' '{ = FILENAME OFS }1' file1.txt file2.txt
file1.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file1.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file1.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3
file2.txt | SERVICE: 1 | TASK: 1 | RESULT: 1 | ADDITIONAL: 1
file2.txt | SERVICE: 2 | TASK: 2 | RESULT: 2 | ADDITIONAL: 2
file2.txt | SERVICE: 3 | TASK: 3 | RESULT: 3 | ADDITIONAL: 3