我想格式化从 macbook 终端命令生成的输出system_profiler
,并将行值转置到同一应用程序下的列。
这是该文件的片段:
火狐浏览器:
Version: 32.0.3
Obtained from: Identified Developer
Last Modified: 28/09/2014 18:23
Kind: Intel
64-Bit (Intel): Yes
Signed by: Developer ID Application: Mozilla Corporation, Developer ID Certification
Location: /Applications/Firefox.app
Get Info String: Firefox 32.0.3
Calendar:
Version: 7.0
Obtained from: Apple
Last Modified: 18/03/2014 06:58
Kind: Intel
64-Bit (Intel): Yes
Location: /Applications/Calendar.app
...并希望输出如下图所示:
笔记:
- 并非每个申请都会填写所有栏目
- 此类信息最多有 8 个(请参阅 Firefox 应用程序下的完整集)
- 每个应用程序信息组之间的前导填充空格和换行符
答案1
awk -F: ' # set fields separator to :
$1!="" && $2==""{i++;NAME[i]=$1} # if 1st field without second it is app.name
$2!=""{HEAD[$1]=1;DATA[NAME[i],$1]=$2 $3} # put variables name into array
#+ put variables into 2-dimention array app.name,var.name
END{
printf("%s:","Application"); # first column in header
for (n in HEAD) printf("%s:",n); # print all var.name as header separated by :
print ""; # finish line by newline
for(i in NAME){ # for each app.name
printf("%s:",NAME[i]); # print app.name
for (n in HEAD) printf("%s:", DATA[NAME[i],n]); # print each variables corresponding to app.name
print ""; # finish line by newline
}
}' data.file | sed -E 's/:\s*/:/g' | column -s: -tn
sed
删除之后的所有空格:
column
设置文本列的格式