コンテンツを次の形式に変換するには?

コンテンツを次の形式に変換するには?
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

関連情報