Korn/Bash Shell: 콘텐츠를 다음 형식으로 변환하는 방법은 무엇입니까?

Korn/Bash Shell: 콘텐츠를 다음 형식으로 변환하는 방법은 무엇입니까?
 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,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

답변1

sed '
    # delete the 2nd line
    2d

    # remove any leading whitespace
    s/^[[:blank:]]\+//

    # on line 1, replace "data" with other words
    1s/data/appnbr request tasknbr appctx username/

    # replace any sequences of whitespace with comma
    s/[[:blank:]]\+/,/g

    # replace the 3rd and subsequent colons
    s/:/,/3g
' file

s///3g이 작업에는 GNU sed가 필요합니다.

답변2

이 시도

grep -v "^-" test.txt | tr -s " " ',' |  sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'

답변3

$ awk -f script.awk file.txt
pid,name,tid,mod,state,appnbr,request,tasknbr,appctx,username
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

script.awk어디

BEGIN   { OFS = "," } # set output delimiter

NR == 1 {
    # modify some fields of the header
    $6 = "appnbr"
    $7 = "request"
    $8 = "tasknbr"
    $9 = "appctx"
    $10 = "username"
}

NR == 2 { next } # skip line 2

NR > 2 {
    # split the sixth field on ":" and extend the record with the bits
    split($6, a, ":")
    $6 = a[1]
    $7 = a[2]
    $8 = a[3]
}

1 # print

답변4

이것이 나의 awk시도이다.

awk 'BEGIN{print "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"}NR>2{print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)}' file.txt
  • NR>2 - 레코드 번호 1과 2(헤더)를 건너뜁니다.
  • $1~$5 필드를 쉼표로 인쇄하세요.
  • $6 필드를 인쇄하는 대신 :","로 바꿔서 인쇄하세요.

관련 정보