我试图解析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的状态来完成,当它们都被设置时打印出来,然后清空它们,但是我不知道该如何做。
发布于 2019-07-11 12:02:39
你能试一下吗。
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发布于 2019-07-11 14:24:13
你们很亲密:
$ 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中没有包括:之后的空白,因此每个字段中仍然存在该块。
发布于 2019-07-11 12:31:16
我写了这个sed脚本:
pacman -Qi | sed -n '/^\(Name\|Description\)[[:space:]]*: /{s///;H}; /^$/ba; $ba; d; :a;x;s/^\n//;s/\n/\t/;p'/^\(Name\|Description\)[[:space:]]*: /{s///;H}; /^$/ba --以Name和Description开头的每一行都删除了已删除的部分,并追加以容纳空间。/^$/ba; $ba; d; --如果遇到一个空行或文件末尾,我们会分支来标记a;否则就会启动新的循环。: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 filehttps://stackoverflow.com/questions/56988456
复制相似问题