用awk打印表格中的空白

时间:2019-07-17 09:28:06

标签: awk

我有一个看起来像这样的图案。

 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=

要添加这些空白字段的另一件事将不时具有一些值。

2 个答案:

答案 0 :(得分:0)

这可能会做到:

$scope.roomsFilter = function (bolig) {
        var rooms = parseFloat(bolig.rooms);
        var number = parseFloat($scope.searchRooms);

        if (!rooms) {
            return false;
        }
        if (number === 5 && rooms < number) {
            return false;
        } else if (number && rooms !== number && number !== 5) {
            return false;
        }
        return true;
    };

NO_ACTION很难处理,但是可以使用固定文件宽度awk 'NR==1 {for (i=1;i<=NF;i++) a[i]=$i;c=NF;next} NR>2 {for (i=1;i<=c;i++) printf "%s=%s,",a[i],$i;print ""}' file 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=, No=1,Type=DIA,Pid=10903,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=2,Type=DIA,Pid=10909,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=3,Type=DIA,Pid=10916,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=4,Type=DIA,Pid=10917,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=5,Type=DIA,Pid=9061,Status=Wait,Cause=yes,Start=no,Rstr=1,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=6,Type=DIA,Pid=10919,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=7,Type=DIA,Pid=10920,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=8,Type=UPD,Pid=10921,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=9,Type=BTC,Pid=24376,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=10,Type=BTC,Pid=25651,Status=Wait,Cause=yes,Start=no,Rstr=1,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=11,Type=BTC,Pid=25361,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, No=12,Type=BTC,Pid=15201,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=, 来完成。但是由于标头未与数据对齐,因此使用简单的命令可能很难。

答案 1 :(得分:0)

没有关于数据外观的明确信息。我们不知道您的数据是以制表符分隔(很好)还是仅以空格分隔。如示例所示,如果用空格分隔,则很难区分空列。

我看到的唯一区分空列的方法是假设输入文件的 header 与相应的列对齐,因此我们可以利用它来发挥自己的优势。以下解决方案适用于GNU awk 4.2或更高版本

有一个文件convert.awk,其中包含以下内容:

BEGIN{ OFS="," }
# Read header and find the starting index of each column
# and the corresponding length
# We assume that the headers are uniquely defined.    
(FNR==1) { 
   h[1]=$1; l=1
   for (i=2;i<=NF;++i) { 
       h[i]=$i; t=index($0,$i); f=f " "(t-l); l=t
   }
   n=NF; FIELDWIDTHS = f " *"
   next
}
# skip ruler
/^[-]+$/ { next }
# print record
{
    for (i=1;i<=n;++i) {
        t=(i>NF ? "" : $i); gsub("(^ *| *$)","",t)
        printf "%s%s=%s",(i==1?"":OFS),h[i],t
    }
    printf ORS
}

并运行:

$ awk -f convert.awk input > output

这将输出:

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=
No=1,Type=DIA,Pid=10903,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=2,Type=DIA,Pid=10909,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=3,Type=DIA,Pid=10916,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=4,Type=DIA,Pid=10917,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=5,Type=DIA,Pid=9061,Status=Wait,Cause=,Start=yes,Rstr=no,Err=1,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=6,Type=DIA,Pid=10919,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=7,Type=DIA,Pid=10920,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=8,Type=UPD,Pid=10921,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=9,Type=BTC,Pid=24376,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=10,Type=BTC,Pid=25651,Status=Wait,Cause=,Start=yes,Rstr=no,Err=1,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=11,Type=BTC,Pid=25361,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=12,Type=BTC,Pid=15201,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=13,Type=BTC,Pid=5241,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=14,Type=BTC,Pid=23572,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=15,Type=BTC,Pid=8603,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=16,Type=BTC,Pid=1418,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=17,Type=BTC,Pid=18127,Status=Wait,Cause=,Start=yes,Rstr=no,Err=1,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=18,Type=BTC,Pid=14780,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=19,Type=BTC,Pid=18234,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=20,Type=BTC,Pid=14856,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=21,Type=SPO,Pid=10934,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=
No=22,Type=UP2,Pid=10939,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=