grep或awk,如何将数据转换为csv文件中的多列

时间:2018-11-07 07:42:11

标签: bash awk grep

我有以下日志文​​件数据

2/1/1/2/tasdfn.c:

LOG:
        backslash-newline should be deleted before tokenizing
    No diagnostics line
--
2/1/1/2/tlsdf.c:

LOG:
+++ stderr ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tlsdf.c:15:5: error: use of undeclared identifier 'wchar_t'
--
2/2/4/1/tasdf.gen(0):

LOG:
    511 external identifiers in one source file
    Compilation failed ungracefully
--
2/2/4/1/tiasdf.gen(0):

LOG:
    8 nesting levels of #include files
    Compilation failed ungracefully
--

并希望将以上数据隐藏起来:coulmn1应该testname(即2/1/1/2 / testname.c)和coulmn2应该LOG详细信息:

csv文件中的预期输出:

2/1/1/2/tasdfn.c:           LOG:
                            backslash-newline should be deleted before tokenizing
                            No diagnostics line


2/1/1/2/tlsdf.c:            LOG:
                            +++ stderr ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                            tlsdf.c:15:5: error: use of undeclared identifier 't'



2/2/4/1/tasdf.gen(0):       LOG:
                            511 external identifiers in one source file
                            Compilation failed ungracefully

无法从grepawk获得所需的输出。 使用grep我可以获取testname即日志详细信息作为单独的输出

cat test.log | grep -Ea '*.c:$|*.gen\([0-9]\):' //prints testname
cat test.log | grep -Ea '*.c:$|*.gen\([0-9]\):' -A4 //prints testname with Log details

如何获取提到的.csv文件作为输出格式的数据?

1 个答案:

答案 0 :(得分:0)

更新后:

awk '/^--/{printf "\n\n"; f=0; next }
     !f { printf "%-29s%s\n", $0, "LOG:"; f=1; next}
     /^$/{next}
     /LOG:/{next}
     {sub(/ */,""); printf "%-29s%s\n", "", $0}' file

这很接近您想要的内容,但是我不得不在您的第二条日志消息中添加一个额外的空间以使其正常工作:

awk 'BEGIN{RS="\n> -- *\n *"; FS="  +" }
     { gsub(/\n>|LOG:/,"") }
     { sub(/^ /,"",$1); sub(/:*$/,":",$1); printf "%-29s%s\n",$1,"LOG:"}
     { for(i=2;i<=NF; ++i) printf "%-29s%s\n","",$i; printf "\n\n" }' file

这个想法是让awk定义由\n> -- *\n *分隔的记录和由多个空格分隔的字段(这就是为什么我需要在+++++++++++...之后添加额外的空间)。

输出:

2/1/1/2/tasdfn.c:            LOG:
                             backslash-newline should be deleted before tokenizing
                             No diagnostics line


2/1/1/2/tlsdf.c:             LOG:
                             +++ stderr ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                             tlsdf.c:15:5: error: use of undeclared identifier 't'


2/2/4/1/tasdf.gen(0):        LOG:
                             511 external identifiers in one source file
                             Compilation failed ungracefully


2/2/4/1/tiasdf.gen(0):       LOG:
                             8 nesting levels of #include files
                             Compilation failed ungracefully --

原始输入

> 2/1/1/2/tasdfn.c:
> 
> LOG:      backslash-newline should be deleted before tokenizing   No
> diagnostics line
> -- 
>2/1/1/2/tlsdf.c:
> 
> LOG:
> +++ stderr 
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
>tlsdf.c:15:5: error: use of undeclared identifier 't'
> --
> 2/2/4/1/tasdf.gen(0):
> 
> LOG:  511 external identifiers in one source file     Compilation failed
> ungracefully
> -- 
>2/2/4/1/tiasdf.gen(0)::
> 
> LOG:  8 nesting levels of #include files  Compilation failed
> ungracefully
> --