在unix中重新格式化txt文件

时间:2015-08-02 15:53:11

标签: bash shell unix awk scripting

我有一个文件包含进程的名称,它死亡的次数以及它死亡的原因。该文件如下所示:

  26 posix.cc:529 ---PROCESS: cat

  18 XtWindowOfObject () from /usr/lib64/libXt.so.6 ---PROCESS: dog

  16 _XtAddCallbackOnce () from /usr/lib64/libXt.so.6 ---PROCESS: dog

  16 Matrix.cc:4399 ---PROCESS: cat

  9 vtable for CollapsibleEvent () from /srg/release/lib.so ---PROCESS: bird

  1 raise () from /lib64/libc.so.6 ---PROCESS: dog

  1 raise () from /lib64/libc.so.6 ---PROCESS: cat

  1 ?? () ---PROCESS: cat

我想使用一个重新排列它的命令,因此它有如下三列:

  CAT PROCESS CORES:
  cat     26     posix.cc:529
  cat     16     Matrix.cc:4399
  cat     1      raise () from /lib64/libc.so.6
  cat     1      ?? ()

  DOG PROCESS CORES:
  dog     18     XtWindowOfObject () from /usr/lib64/libXt.so.6
  dog     16     _XtAddCallback () from /usr/lib64/libXt.so.6
  dog     1      raise () from /lib64/libc.so.6

 BIRD PROCESS CORES: 
  bird    9     vtable for CollapsibleEvent () from /srg/release/lib.so

随着文件每天生成,进程名称总是在变化。请帮助 - 我一直在尝试使用awk,但我无法解决如何正确设置分隔符的问题。

2 个答案:

答案 0 :(得分:2)

您可以使用perl:

/* can write here "inline" */ void this_function ( ) 
{
    return ;
}

int main ( void )
{
    this_function () ; // here this function must be compiled as inline
    this_function () ; // but not here ; now it must be called
}

或可读

perl -lanE 'if(/\w/){push @{$r{@F[-1]}},[$F[0],"@F[1..$#F-2]"]}}{for$k(sort keys%r){say uc($k)." PROCESS CORES:";say join"\t",$k,@{$_}for@{$r{$k}}}' < input_file.txt

产生

perl -lanE '
if( /\w/ ) { #if the line contains any word character
        push @{$r{@F[-1]}}, [ $F[0], "@F[1 .. $#F-2]" ] #store by last word
}
}{ #eskimo
for $k (sort keys %r) { #for each type
        say uc($k)." PROCESS CORES:";
        say join "\t", $k, @{$_} for @{$r{$k}} #print lines
}
' < input_file.txt

答案 1 :(得分:1)

你可以尝试这个awk命令:

awk '
/./{ process = $NF
     split($0,info,/---PROCESS:/)
     accumulate[process] = accumulate[process] process " " info[1] "\n"
}
END {
  n = asorti(accumulate, sorted)
  for(i = 1;i<=n;i++){
    process = sorted[i]
    caps = toupper(process)
    printf "\n%s PROCESS CORES:\n%s\n",caps, accumulate[process]
  }
}'