如何使用awk解析多行而没有奇怪的重复项

时间:2019-07-11 11:46:41

标签: bash shell awk pattern-matching

我正在尝试解析pacman -Qi的输出,看起来或多或少像这样:

Name            : zvbi
Version         : 0.2.35-3
Description     : VBI capture and decoding library
Build Date      : Fri 24 Aug 2018 09:48:59 CEST
Install Date    : Thu 30 Aug 2018 08:55:50 CEST
Install Reason  : Installed as a dependency for another package
Install Script  : No
Validated By    : Signature

Name            : zziplib
Version         : 0.13.69-1
Description     : A lightweight library that offers the ability to easily extract data from files archived in a single zip file
Build Date      : Wed 21 Mar 2018 21:16:20 CET
Install Date    : Thu 22 Mar 2018 11:13:19 CET
Install Reason  : Installed as a dependency for another package
Install Script  : No
Validated By    : Signature

我需要将其解析为:

zvbi    VBI capture and decoding library
zziplib    A lightweight library that offers the ability to easily extract data from files archived in a single zip file

空格是制表符

现在我尝试用以下方法解析它: pacman -Qi | awk -F: '/^Name/ {n=$2} /^Desc/ {d=$2} {print n "\t" d}'

但是它输出了

 zvbi   
 zvbi   
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zvbi    VBI capture and decoding library
 zziplib     VBI capture and decoding library
 zziplib     VBI capture and decoding library
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file

使用uniq可以做到这一点

 zvbi   
 zvbi    VBI capture and decoding library
 zziplib     VBI capture and decoding library
 zziplib     A lightweight library that offers the ability to easily extract data from files archived in a single zip file

请注意每行开头的空格。

我认为可以通过检查变量n和d的状态来完成,在它们都被设置时打印它们,然后清空它们,但是我不确定该怎么做。

3 个答案:

答案 0 :(得分:2)

请您尝试以下。

awk '
BEGIN{
  OFS="\t"
}
/^Name/{
  if(value){
     print value
  }
  sub(/.*: /,"")
  value=$0
  next
}
/^Description/{
  sub(/.*: /,"")
  value=(value?value OFS:"")$0
}
END{
  if(value){
    print value
  }
}
'  Input_file

答案 1 :(得分:1)

您很亲密:

$ awk -F': ' '/^Name/ {n=$2} /^Desc/ {print n "\t" $2}' file
zvbi    VBI capture and decoding library
zziplib A lightweight library that offers the ability to easily extract data from files archived in a single zip file

脚本的主要问题是{print...}块是针对每一行输入执行的,而不是仅在看到Desc行时才执行,然后在:在您的FS中,因此它仍然存在于每个字段中。

答案 2 :(得分:0)

我已经写了这个sed脚本:

pacman -Qi | sed -n '/^\(Name\|Description\)[[:space:]]*: /{s///;H}; /^$/ba; $ba; d; :a;x;s/^\n//;s/\n/\t/;p'
  1. /^\(Name\|Description\)[[:space:]]*: /{s///;H}; /^$/ba-每行以NameDescription开头的行都删除了Macthed部分,并用于保留空间。
  2. /^$/ba; $ba; d;-如果遇到空行或文件结尾,我们分支以标记a;否则,我们将开始新的循环。
  3. :a;x;s/^\n//;s/\n/\t/;p-在标签a中,我们将保留空间与模式空间交换,删除开头的换行符(不知道它来自何处),用制表符替换第一条换行符,然后输出输出。

示例输出:

zlib    Compression library implementing the deflate compression method found in gzip and PKZIP
zsh A very advanced and programmable command interpreter (shell) for UNIX
zstd    Zstandard - Fast real-time compression algorithm
zvbi    VBI capture and decoding library
zziplib A lightweight library that offers the ability to easily extract data from files archived in a single zip file