如何将内容转换为以下格式?

如何将内容转换为以下格式?
pid        name          tid        mod         state   data
-------------------------------------------------------------------------  
39523      srv0051_0001_0  39642 20-10:59:28 Working 820000:500196:500077 
43137      srv0051_0005_0  43156 20-10:59:28 Working 820000:4250501:840057
43895      srv0051_0006_0  43903 20-10:59:28 Working 820000:4250501:840057
47523      srv0051_0009_0  47547 20-10:59:28 Working 600005:4250501:4250846
48841      srv0051_0010_0  48851 20-10:59:28 Working 600005:4290000:4290000
58182      srv0051_0020_0  58188 20-10:59:28 Working 820000:4250501:840057
8297       srv0079_0008_0  8316  20-10:59:27 Working 600005:3070001:3050012

pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,09-JUL-2018 12:36:10,Working,820000,500196,500077
43137,srv0051_0005_0,43156,09-JUL-2018 12:36:10,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,09-JUL-2018 12:36:10,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,09-JUL-2018 12:36:10,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,09-JUL-2018 12:36:10,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,09-JUL-2018 12:36:10,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,09-JUL-2018 12:36:10,Working,600005,3070001,3050012

这些数据是通过运行以下命令收集的:

spsmon -state working -snapshot > /root/file.txt

到目前为止我所拥有的:

awk 'BEGIN{print 
"pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"} NR>5 
'!/Process/' '!/ProcessModules/' '!/ProcessEvoke/' {print 
$1","$2","$3","$4","$5","gensub(/:/,",","g",$6)}' /root/file.txt > 
/root/file.txt

我主要对如何将 mod 列更改为我想要的日期格式感到困惑。

答案1

使用 GNU Awk,并从当前时间获取缺少的月份和年份:

gawk '
  BEGIN{
    OFS=","
    print "pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"
  }
  NR>2 {
    split($4,a,"-")
    $4 = a[1] strftime("-%^b-%Y ", systime()) a[2]
    gsub(/:/,",",$6)
    print
  }' /root/file.txt

或省略该文件并在管道中使用它

spsmon -state working -snapshot | gawk '...'

前任。

$ gawk '
  BEGIN{
    OFS=","
    print "pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"
  }
  NR>2 {
    split($4,a,"-")
    $4 = a[1] strftime("-%^b-%Y ", systime()) a[2]
    gsub(/:/,",",$6)
    print
  }' file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
39523,srv0051_0001_0,39642,20-JUL-2018 10:59:28,Working,820000,500196,500077
43137,srv0051_0005_0,43156,20-JUL-2018 10:59:28,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,20-JUL-2018 10:59:28,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,20-JUL-2018 10:59:28,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,20-JUL-2018 10:59:28,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,20-JUL-2018 10:59:28,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,20-JUL-2018 10:59:27,Working,600005,3070001,3050012

答案2

鉴于源数据中没有任何明显的内容似乎是 的起源09-JUL-2018,我已对其进行了硬编码。那是说:

$  awk -F"[ \t]+|[:]" 'BEGIN { OFS=","; print "pid,name,tid,mod,state,appnbr,request,tasknbr,appctz,username" } NR>2 {print $1,$2,$3,"09-JUL-2018",$7, $8, $9, $10}' input
pid,name,tid,mod,state,appnbr,request,tasknbr,appctz,username
39523,srv0051_0001_0,39642,09-JUL-2018,Working,820000,500196,500077
43137,srv0051_0005_0,43156,09-JUL-2018,Working,820000,4250501,840057
43895,srv0051_0006_0,43903,09-JUL-2018,Working,820000,4250501,840057
47523,srv0051_0009_0,47547,09-JUL-2018,Working,600005,4250501,4250846
48841,srv0051_0010_0,48851,09-JUL-2018,Working,600005,4290000,4290000
58182,srv0051_0020_0,58188,09-JUL-2018,Working,820000,4250501,840057
8297,srv0079_0008_0,8316,09-JUL-2018,Working,600005,3070001,3050012

答案3

使用 sed:

 sed -e 's/  */,/g;s/:/,/g;/^-/d;s/data/appnbr,request,tasknbr,appctx,username/1;s/20-10:59:28/09-JUL-2018 12:36:10/g' /root/file.txt

相关内容