在linux中合​​并和对齐行 [英] merging and aligning the line in linux

查看:111
本文介绍了在linux中合​​并和对齐行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为file1.txt的文本文件,它需要像下面的预期输出一样对齐. 注意:输入文件file1.txt被禁止使用TAB,并且始终对齐.

I have a text file named as file1.txt .and its need to be aligned like below expected output . Note:The input file file1.txt is TAB delemitted and its always aligned.

                      DISK OK - free space:          CRITICAL
01-08-2018 07:05:05   Service Required     Critical  CPU:loadaverage 6.0%                    

01-08-2018 07:10:25   Service Alert        Critical  memoryUsage
                                                     :critical alert
                       DISK OK - free space:
02-08-2018 01:05:20    Service Alert       Warning   memoryUsage
                                                     :1.0,2.0,5.0


                                                     CRITICAl:outstanding alert attention 
02-08-2018 02:05:20    Service Alert       Critical  required 

预期输出:

 01-08-2018 07:05:05    DISK OK - free space:Service Required Critical    CRITICALservice requiredCPU:loadaverage 6.0%

 01-08-2018 07:10:25    Service Alert                         Critical    memoryUsage:critical alert

 02-08-2018 01:05:20    DISK OK - free space:Service Alert    Warning     memoryUsage:1.0,2.0,3.0

 02-08-2018 02:05:20    Service Alert                         Critical     CRITICAL:outstanding alert attention required

任何建议都值得赞赏.

推荐答案

您可以使用awk将其完成:

 awk -F"\t" '!$1{for(i=1;i<=NF;i++){line[i]=$i};next} {for(i=1;i<=NF;i++){printf i<NF?"%s%s%s":"%s%s%s\n",line[i],$i,FS}}' yourfile

如果您需要输出像此处一样对齐(没有制表符分隔,但更像固定宽度),则可以通过管道传输到column

If you need the output all aligned like you have it here (not tab delimited, but more like fixed width) you can pipe to column

awk -F"\t" '!$1{for(i=1;i<=NF;i++){line[i]=$i}} $1{for(i=1;i<=NF;i++){printf i<NF?"%s%s%s":"%s%s%s\n",line[i],$i,FS}}' test.log | column -t -s $'\t'

基本上这是在做什么:

  1. 通过标签-F"\t"
  2. 分隔每行
  3. 如果第一列为空,则以该列号作为索引将该行的每一列收集到一个数组中,并继续进行下一条记录!$1{for(i=1;i<=NF;i++){line[i]=$i};next}
  4. 如果我们仍在处理该行(第一个条件未触发),则遍历每列{for(i=1;i<=NF;i++)
  5. 并打印出数组中存储的内容,当前行的列内容,字段分隔符(选项卡)和换行符(如果这是最后一列){printf i<NF?"%s%s%s":"%s%s%s\n",line[i],$i,FS}}
  1. Splitting each line by tab -F"\t"
  2. If the first column is empty, then collect each column for this line into an array by the column number as index and proceed to the next record!$1{for(i=1;i<=NF;i++){line[i]=$i};next}
  3. If we are still processing the line (that first condition didn't trip) then go through each column {for(i=1;i<=NF;i++)
  4. and print out what's stored in the array, the contents of the column of the current line, the Field Separator (tab), and a line feed if this is the last column {printf i<NF?"%s%s%s":"%s%s%s\n",line[i],$i,FS}}

正在使用的示例:

$ cat test.log
        DISK OK - free space:           CRITICAL
1/8/2018 7:05:05        Service Required        Critical        CPU:loadaverage 6%

1/8/2018 7:10:25        Service Alert   Critical        memoryUsage
                        :critical alert
        DISK OK - free space:
2/8/2018 1:05:20        Service Alert   Warning memoryUsage
                        :1.0,2.0,5.0


                        CRITICAl:outstanding alert attention
2/8/2018 2:05:20        Service Alert   Critical        required

$ awk -F"\t" '!$1{for(i=1;i<=NF;i++){line[i]=$i};next} {for(i=1;i<=NF;i++){printf i<NF?"%s%s%s":"%s%s%s\n",line[i],$i,FS}}' test.log | column -t -s $'\t'
1/8/2018 7:05:05  DISK OK - free space:Service Required  Critical  CRITICALCPU:loadaverage 6%
1/8/2018 7:10:25  Service Alert                          Critical  memoryUsage
2/8/2018 1:05:20  DISK OK - free space:Service Alert     Warning   memoryUsage
2/8/2018 2:05:20  Service Alert                          Critical  CRITICAl:outstanding alert attentionrequired

这篇关于在linux中合​​并和对齐行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆