我有一个看起来像这样的图案。
No Type Pid Status Cause Start Rstr Err Sem Time Program Cl User Action Table
-------------------------------------------------------------------------------------------------------------------------------
0 DIA 10897 Wait yes no 0 0 0 NO_ACTION
1 DIA 10903 Wait yes no 0 0 0 NO_ACTION
2 DIA 10909 Wait yes no 0 0 0 NO_ACTION
3 DIA 10916 Wait yes no 0 0 0 NO_ACTION
4 DIA 10917 Wait yes no 0 0 0 NO_ACTION
5 DIA 9061 Wait yes no 1 0 0 NO_ACTION
6 DIA 10919 Wait yes no 0 0 0 NO_ACTION
7 DIA 10920 Wait yes no 0 0 0 NO_ACTION
8 UPD 10921 Wait yes no 0 0 0 NO_ACTION
9 BTC 24376 Wait yes no 0 0 0 NO_ACTION
10 BTC 25651 Wait yes no 1 0 0 NO_ACTION
11 BTC 25361 Wait yes no 0 0 0 NO_ACTION
12 BTC 15201 Wait yes no 0 0 0 NO_ACTION
13 BTC 5241 Wait yes no 0 0 0 NO_ACTION
14 BTC 23572 Wait yes no 0 0 0 NO_ACTION
15 BTC 8603 Wait yes no 0 0 0 NO_ACTION
16 BTC 1418 Wait yes no 0 0 0 NO_ACTION
17 BTC 18127 Wait yes no 1 0 0 NO_ACTION
18 BTC 14780 Wait yes no 0 0 0 NO_ACTION
19 BTC 18234 Wait yes no 0 0 0 NO_ACTION
20 BTC 14856 Wait yes no 0 0 0 NO_ACTION
21 SPO 10934 Wait yes no 0 0 0 NO_ACTION
22 UP2 10939 Wait yes no 0 0 0 NO_ACTION
现在我使用 awk 将其转换为如下所示
NO=0,Type=DIA,Pid=10897,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=
上面是一行的示例,所有行都相同。
我们在运行时通过 sed 命令删除列标题,现在当我们使用 awk 时,它会丢失状态和原因之间的空格,并写入应在原因处开始的值。
我们正在使用以下命令。
awk 'BEGIN{FS=" ";OFS=","}{print "NO="$1,"Type="$2,"Pid="$3,"Status="$4,"Cause="$5,"Start="$6,"Rstr="$7,"Err="$8,"Sem="$9,"Time="$10,"Program="$11,"Cl="$12,"User="$13,"Action="$14,"Table="$15;}'
我们希望输出是这样的
NO=0,Type=DIA,Pid=10897,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
添加这些空白字段的另一件事是不时会有一些值。