【问题标题】:awk command to parse file seperated by new lines用于解析由新行分隔的文件的 awk 命令
【发布时间】:2020-02-05 07:35:31
【问题描述】:

我的输入文件如下:

OS ABI

   UNIX - System V

CPU Class

   64-bit

Persistence (user)

   No

File type 

   ELF 32-bit MSB executable, PowerPC or cisco 4500, version 
   (SYSV),statically linked, not   stripped,32-bit MSB executable, 
   PowerPC or cisco 4500, version (SYSV),statically linked, not stripped

CPU Type

  PowerPC

我想输出为:

OS ABI UNIX - System V
CPU CLASS 64-bit
Persistence (user) No
File Type ELF 32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not   stripped,32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not stripped 
CPU Type PowerPC

请建议使用 awk 的脚本/命令可以输出所需的结果?当我们必须解析多行时,问题就出现了,如字段 5 所示。我还希望计算每个字段中的行数,例如:字段 1 有一行,字段 5 有 2 等等。

【问题讨论】:

    标签: awk newline


    【解决方案1】:

    这将为您完成:

    awk 'BEGIN{RS=""}
         !/^[[:blank:]]/{printf "%s" OFS, $0; next}
         { gsub(/(^|\n)[[:blank:]]+/,OFS) }1' file
    

    此解决方案只会删除偶数块开头的间距。这与将所有间距替换为单个间距的其他解决方案相反。

    【讨论】:

      【解决方案2】:

      编辑:由于 OP 更改了 Input_file,因此根据新示例在此处添加了一个解决方案。

      awk -v RS="" -v FS="\n" '
      {
        gsub(/\n+/,OFS)
      }
      FNR%2==0{
        print value,$0
        next
      }
      {
        value=$0
      }
      '  Input_file
      

      说明:为上述代码添加详细说明。

      awk -v RS="" -v FS="\n" '     ##Setting RS(record separator) as NULL and setting FS(field separator) as new line.
      {
        gsub(/\n+/,OFS)             ##Globally substituting one or more lines occurrence with space here.
      }
      FNR%2==0{                     ##Checking condition if line number is even then do following.
        print value,$0              ##Printing value and current line here.
        next                        ##next will skip all further statements from here.
      }
      {
        value=$0                    ##Assigning current line value to variable value here.
      }
      ' Input_file                  ##Mentioning Input_file name here.
      


      您能否尝试使用提供的示例进行跟踪、编写和测试。

      awk '
      NF && /^Field/{
        if(field_value && value){
          print field_value,value
        }
        value=""
        field_value=$0
        next
      }
      NF{
        value=(value?value OFS:"")$0
      }
      END{
        if(field_value && value){
          print field_value,value
        }
      }
      '  Input_file
      

      【讨论】:

        【解决方案3】:

        另一个awk 变体:

        awk '/^[^ \t]/{if (p != "") print p; p=$0} 
        /^[ \t]/{sub(/^[ \t]+/, ""); p = p " " $0} END{print p}' file
        

        OS ABI UNIX - System V
        CPU Class 64-bit
        Persistence (user) No
        File type  ELF 32-bit MSB executable, PowerPC or cisco 4500, version  (SYSV),statically linked, not   stripped,32-bit MSB executable,  PowerPC or cisco 4500, version (SYSV),statically linked, not stripped
        CPU Type PowerPC
        

        或者如果你可以使用perl,那么就使用这个单行:

        perl -0777 -pe 's/(?:\h*\R+)+\h+/ /g; s/\R(?:\h*\R)+/\n/g' file
        

        【讨论】:

          【解决方案4】:
          $ awk -v RS= 'NR%2{p=$0; next} {$0=p $0; $1=$1} 1' file
          OS ABI UNIX - System V
          CPU Class 64-bit
          Persistence (user) No
          File type ELF 32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not stripped,32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not stripped
          CPU Type PowerPC
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-01-25
            • 1970-01-01
            • 2012-12-28
            • 2018-08-15
            • 2021-12-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多