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