首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用awk解析多行而不存在奇怪的重复

如何使用awk解析多行而不存在奇怪的重复
EN

Stack Overflow用户
提问于 2019-07-11 11:46:41
回答 3查看 45关注 0票数 2

我试图解析pacman -Qi的输出,它看起来或多或少如下所示:

代码语言:javascript
复制
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

我需要将其解析为:

代码语言:javascript
复制
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}'解析它

但它输出的是

代码语言:javascript
复制
 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给出了

代码语言:javascript
复制
 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的状态来完成,当它们都被设置时打印出来,然后清空它们,但是我不知道该如何做。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-11 12:02:39

你能试一下吗。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2019-07-11 14:24:13

你们很亲密:

代码语言:javascript
复制
$ 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中没有包括:之后的空白,因此每个字段中仍然存在该块。

票数 1
EN

Stack Overflow用户

发布于 2019-07-11 12:31:16

我写了这个sed脚本:

代码语言:javascript
复制
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开头的每一行都删除了已删除的部分,并追加以容纳空间。
  2. /^$/ba; $ba; d; --如果遇到一个空行或文件末尾,我们会分支来标记a;否则就会启动新的循环。
  3. :a;x;s/^\n//;s/\n/\t/;p -在标签a中,我们用模式空间交换持有空间,删除前导换行符(不知道从哪里来),用选项卡替换第一行,然后打印输出。

样本输出:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56988456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档