- 已关闭**。此问题需要details or clarity。当前不接受答案。
- 想要改进此问题?**添加详细信息并通过editing this post阐明问题。
2天前关闭。
Improve this question
我正在尝试使用awk命令从文件中过滤出数据并将其放入CSV文件。我正在尝试创建列标题,但数据之间有空格,因此脚本将每个字符作为单独的名称。
我正在使用的脚本
$ cat tst.sh
#!/usr/bin/env bash
cat file |
awk '
BEGIN {
OFS = ","
numTags = split("Machine Name Type Node Name Agent Name Operating System Agent Release Agent Build",tags)
for ( tagNr=1; tagNr<=numTags; tagNr++ ) {
tag = tags[tagNr]
printf "\"%s\"%s", tag, (tagNr<numTags ? OFS : ORS)
}
}
!NF || /^\/\*/ { next }
{ gsub(/^[[:space:]]+|[[:space:]]+$/,"") }
match($0,/[[:space:]]job_type:/) {
if ( jobNr++ ) {
prt()
delete tag2val
}
# save "insert_job" value
tag = substr($1,1,length($1)-1)
val = substr($0,length($1)+1,RSTART-(length($1)+2))
gsub(/^[[:space:]]+|[[:space:]]+$/,"",val)
tag2val[tag] = val
# update $0 to start with "job_type" to look like all other input
$0 = substr($0,RSTART+1)
}
{
tag = val = $0
sub(/:.*/,"",tag)
sub(/[^:]+:[[:space:]]*/,"",val)
tag2val[tag] = val
}
END { prt() }
function prt( tagNr,tag,val) {
for ( tagNr=1; tagNr<=numTags; tagNr++ ) {
tag = tags[tagNr]
val = tag2val[tag]
printf "\"%s\"%s", val, (tagNr<numTags ? OFS : ORS)
}
}
'
File
的内容:
$ cat file
Machine Name: machine1
Type: a
Node Name: machine1.test
Agent Name: WA_AGENT
Operating System: Windows Server 2012
Agent Release: 12.0
Agent Build: 6181, Service Pack 00, Maintenance Level 00
Machine Name: machine2
Type: a
Node Name: machine2.test
Agent Name: WA_AGENT
Operating System: Windows Server 2012 for amd64
Agent Release: 12.0
Agent Build: 6181, Service Pack 00, Maintenance Level 00
我得到的输出:
"Machine","Name","Type","Node","Name","Agent","Name","Operating","System","Agent","Release","Agent","Build"
"","","a","","","","","","","","","",""
所需输出:
"Machine Name","Type","Node Name","Agent Name","Operating System","Agent Release","Agent Build"
"machine1"," a"," machine1.test"," AGENT"," Windows Server 2012"," 12.0"," 6181, Service Pack 00, Maintenance Level 00"
"machine2"," a"," machine2.test"," AGENT"," Windows Server 2012"," 12.0"," 6181, Service Pack 00, Maintenance Level 00"
有没有办法得到我想要的输出。
1条答案
按热度按时间0md85ypi1#
忽略一些输出字段中的前导空格作为idk,如果/为什么你想要这些,如果你真的想要,你可以调整这个来添加它们,下面是如何修改你的问题中的代码来做你想要的:
实际上,如果我对这个特定的问题从头开始,我不会在问题中硬编码标记,我只会在每次遇到空行时打印所有的值。
关于我为您提供的任何脚本,有一点需要注意--我不使用$1和$2这样的字段来保存标记或值,因为一旦您这样做,如果您的数据可以包含任何用作FS的内容,您就会遇到问题。
例如,如果您的数据如下所示:
那么就不要在代码中执行类似以下的操作:
因为当值(或者,可能性更小的标签)包含与
FS
匹配的字符串时(例如,本例中的:
),它将失败,例如,给定以下数据:最后
val
会被设置为"the ratio was 2
。因此最后
val
设置为"the ratio was 2:1"
。