Korn / Bash Shell: Como faço para converter o conteúdo para o formato abaixo?

1
 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
    
por fball4life36 25.06.2018 / 21:09

5 respostas

6
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

Necessário sed GNU para a ação s///3g

    
por 25.06.2018 / 21:24
2

Tente isso

grep -v "^-" test.txt | tr -s " " ',' |  sed -e s/:/,/3g -e '0,/data/ s/data/appnbr,request,tasknbr,appctx,username/'
    
por 25.06.2018 / 21:18
1
$ 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

Onde 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
    
por 25.06.2018 / 21:40
1
perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt

Saída:

id,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

Explicação:

  1. Divida cada linha, a.k.a., registro, em espaço (s) de branco e armazene os campos reultantes na matriz @F . A matriz é indexada de 0 e vai até $#F . Então, $#F-1 é o segundo último elemento.
  2. Todas as linhas, exceto a segunda, devem ser processadas, pois o último elemento é o fator decisivo.
  3. Para a primeira linha, o último elemento é substituído por vários nomes de campos.
  4. Para os demais, (lembre-se de que o segundo já foi descartado) são divididos em : e esses campos divididos vêm no lugar do último elemento.
  5. A união de todos os elementos é feita pelo , e depois impressa em stdout .

Com POSIX sed , podemos fazer o seguinte:

sed -e '
   2d
   s/^[[:blank:]]*//;s/[[:blank:]]*$//;s/[[:blank:]]\{1,\}/ /g ;# trim n squeeze blanks
   1s/data$/appnbr request tasknbr appctx username/            ;# line 1 spl processing
   y/:/\n/         ;# change all colons to newlines
   s/\n/:/;s//:/   ;# undo the transformation for the first 2
   y/\n/ /         ;# change the rest to blanks
   y/ /,/          ;# now change these and the already existing blanks to commas
' input-file.txt
    
por 26.06.2018 / 10:25
1

Aqui está minha awk stab.

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 para que os números de registro 1 e 2 (cabeçalhos) sejam ignorados
  • imprime campos de $ 1 a $ 5 com vírgulas
  • em vez do campo de impressão $ 6, imprima com : substituído por ","
por 25.06.2018 / 21:41