Korn/Bash Shell: How to do I convert the contents to the below format?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












 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






share|improve this question

















  • 1




    pray tell, what have you tried so far ?
    – steve
    Jun 25 at 19:13






  • 1




    @steve I'm very new to scripting so I'm not sure where to start.
    – fball4life36
    Jun 25 at 19:18










  • Your header has more fields than the data. Is this on purpose?
    – glenn jackman
    Jun 25 at 19:32










  • OK, which of the two is original and which is the desired result ?
    – Sergiy Kolodyazhnyy
    Jun 26 at 2:01










  • @glennjackman Yes. The second is the desired state.
    – fball4life36
    Jun 26 at 13:26














up vote
1
down vote

favorite












 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






share|improve this question

















  • 1




    pray tell, what have you tried so far ?
    – steve
    Jun 25 at 19:13






  • 1




    @steve I'm very new to scripting so I'm not sure where to start.
    – fball4life36
    Jun 25 at 19:18










  • Your header has more fields than the data. Is this on purpose?
    – glenn jackman
    Jun 25 at 19:32










  • OK, which of the two is original and which is the desired result ?
    – Sergiy Kolodyazhnyy
    Jun 26 at 2:01










  • @glennjackman Yes. The second is the desired state.
    – fball4life36
    Jun 26 at 13:26












up vote
1
down vote

favorite









up vote
1
down vote

favorite











 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






share|improve this question













 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








share|improve this question












share|improve this question




share|improve this question








edited Jun 25 at 19:53









Jeff Schaller

30.8k846104




30.8k846104









asked Jun 25 at 19:09









fball4life36

347




347







  • 1




    pray tell, what have you tried so far ?
    – steve
    Jun 25 at 19:13






  • 1




    @steve I'm very new to scripting so I'm not sure where to start.
    – fball4life36
    Jun 25 at 19:18










  • Your header has more fields than the data. Is this on purpose?
    – glenn jackman
    Jun 25 at 19:32










  • OK, which of the two is original and which is the desired result ?
    – Sergiy Kolodyazhnyy
    Jun 26 at 2:01










  • @glennjackman Yes. The second is the desired state.
    – fball4life36
    Jun 26 at 13:26












  • 1




    pray tell, what have you tried so far ?
    – steve
    Jun 25 at 19:13






  • 1




    @steve I'm very new to scripting so I'm not sure where to start.
    – fball4life36
    Jun 25 at 19:18










  • Your header has more fields than the data. Is this on purpose?
    – glenn jackman
    Jun 25 at 19:32










  • OK, which of the two is original and which is the desired result ?
    – Sergiy Kolodyazhnyy
    Jun 26 at 2:01










  • @glennjackman Yes. The second is the desired state.
    – fball4life36
    Jun 26 at 13:26







1




1




pray tell, what have you tried so far ?
– steve
Jun 25 at 19:13




pray tell, what have you tried so far ?
– steve
Jun 25 at 19:13




1




1




@steve I'm very new to scripting so I'm not sure where to start.
– fball4life36
Jun 25 at 19:18




@steve I'm very new to scripting so I'm not sure where to start.
– fball4life36
Jun 25 at 19:18












Your header has more fields than the data. Is this on purpose?
– glenn jackman
Jun 25 at 19:32




Your header has more fields than the data. Is this on purpose?
– glenn jackman
Jun 25 at 19:32












OK, which of the two is original and which is the desired result ?
– Sergiy Kolodyazhnyy
Jun 26 at 2:01




OK, which of the two is original and which is the desired result ?
– Sergiy Kolodyazhnyy
Jun 26 at 2:01












@glennjackman Yes. The second is the desired state.
– fball4life36
Jun 26 at 13:26




@glennjackman Yes. The second is the desired state.
– fball4life36
Jun 26 at 13:26










5 Answers
5






active

oldest

votes

















up vote
6
down vote



accepted










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


Required GNU sed for the s///3g action






share|improve this answer























  • GNU sed also needed for +. Standard equivalent is 1,
    – Stéphane Chazelas
    Jun 25 at 20:53










  • @glennjackman I'm confuse. How would the entire command look like?
    – fball4life36
    Jun 27 at 13:47










  • Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
    – glenn jackman
    Jun 27 at 13:51










  • Is it because it's a multiline script with comments? Newlines are valid within quotes
    – glenn jackman
    Jun 27 at 13:52










  • @glennjackman How would command look like without the comments?
    – fball4life36
    Jun 28 at 13:26

















up vote
2
down vote













Try this



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





share|improve this answer























  • A tweak needed to cover the conversion of : to , in the final column ?
    – steve
    Jun 25 at 19:29






  • 1




    @steve thanks.. updated
    – SivaPrasath
    Jun 25 at 19:31

















up vote
1
down vote













$ 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


Where script.awk is



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





share|improve this answer




























    up vote
    1
    down vote













    perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt


    Output:



    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


    Explanation:



    1. Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array @F. Array is indexed from 0 and goes all the way to $#F. So $#F-1 is the second last element.

    2. All lines except the 2nd are to be processed, in that the last element is the deciding factor.

    3. For the first line, the last element is replaced by multiple field names.

    4. For the remaining, (remember 2nd is already discarded) are split up on : and these split up fields come in place of the last element.

    5. The joining of all the elements is done by the , and then printed to stdout.

    With POSIX sed, we can do the following:



    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





    share|improve this answer




























      up vote
      1
      down vote













      Here's my awk stab at it.



      awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt


      • NR>2 so that record number 1 and 2 (headers) are skipped

      • print fields $1 thru $5 with commas

      • instead of printing field $6, print it with : replaced with ","





      share|improve this answer























      • How would I do the new headers aspect?
        – fball4life36
        Jun 27 at 19:44










      • now added the headers part.
        – steve
        Jun 27 at 20:25










      • The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
        – fball4life36
        Jun 27 at 21:04










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451825%2fkorn-bash-shell-how-to-do-i-convert-the-contents-to-the-below-format%23new-answer', 'question_page');

      );

      Post as a guest






























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote



      accepted










      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


      Required GNU sed for the s///3g action






      share|improve this answer























      • GNU sed also needed for +. Standard equivalent is 1,
        – Stéphane Chazelas
        Jun 25 at 20:53










      • @glennjackman I'm confuse. How would the entire command look like?
        – fball4life36
        Jun 27 at 13:47










      • Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
        – glenn jackman
        Jun 27 at 13:51










      • Is it because it's a multiline script with comments? Newlines are valid within quotes
        – glenn jackman
        Jun 27 at 13:52










      • @glennjackman How would command look like without the comments?
        – fball4life36
        Jun 28 at 13:26














      up vote
      6
      down vote



      accepted










      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


      Required GNU sed for the s///3g action






      share|improve this answer























      • GNU sed also needed for +. Standard equivalent is 1,
        – Stéphane Chazelas
        Jun 25 at 20:53










      • @glennjackman I'm confuse. How would the entire command look like?
        – fball4life36
        Jun 27 at 13:47










      • Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
        – glenn jackman
        Jun 27 at 13:51










      • Is it because it's a multiline script with comments? Newlines are valid within quotes
        – glenn jackman
        Jun 27 at 13:52










      • @glennjackman How would command look like without the comments?
        – fball4life36
        Jun 28 at 13:26












      up vote
      6
      down vote



      accepted







      up vote
      6
      down vote



      accepted






      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


      Required GNU sed for the s///3g action






      share|improve this answer















      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


      Required GNU sed for the s///3g action







      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited Jun 25 at 19:33


























      answered Jun 25 at 19:24









      glenn jackman

      45.6k264100




      45.6k264100











      • GNU sed also needed for +. Standard equivalent is 1,
        – Stéphane Chazelas
        Jun 25 at 20:53










      • @glennjackman I'm confuse. How would the entire command look like?
        – fball4life36
        Jun 27 at 13:47










      • Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
        – glenn jackman
        Jun 27 at 13:51










      • Is it because it's a multiline script with comments? Newlines are valid within quotes
        – glenn jackman
        Jun 27 at 13:52










      • @glennjackman How would command look like without the comments?
        – fball4life36
        Jun 28 at 13:26
















      • GNU sed also needed for +. Standard equivalent is 1,
        – Stéphane Chazelas
        Jun 25 at 20:53










      • @glennjackman I'm confuse. How would the entire command look like?
        – fball4life36
        Jun 27 at 13:47










      • Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
        – glenn jackman
        Jun 27 at 13:51










      • Is it because it's a multiline script with comments? Newlines are valid within quotes
        – glenn jackman
        Jun 27 at 13:52










      • @glennjackman How would command look like without the comments?
        – fball4life36
        Jun 28 at 13:26















      GNU sed also needed for +. Standard equivalent is 1,
      – Stéphane Chazelas
      Jun 25 at 20:53




      GNU sed also needed for +. Standard equivalent is 1,
      – Stéphane Chazelas
      Jun 25 at 20:53












      @glennjackman I'm confuse. How would the entire command look like?
      – fball4life36
      Jun 27 at 13:47




      @glennjackman I'm confuse. How would the entire command look like?
      – fball4life36
      Jun 27 at 13:47












      Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
      – glenn jackman
      Jun 27 at 13:51




      Your confusion confuses me: that's the whole thing right there. Copy and paste it into your terminal.
      – glenn jackman
      Jun 27 at 13:51












      Is it because it's a multiline script with comments? Newlines are valid within quotes
      – glenn jackman
      Jun 27 at 13:52




      Is it because it's a multiline script with comments? Newlines are valid within quotes
      – glenn jackman
      Jun 27 at 13:52












      @glennjackman How would command look like without the comments?
      – fball4life36
      Jun 28 at 13:26




      @glennjackman How would command look like without the comments?
      – fball4life36
      Jun 28 at 13:26












      up vote
      2
      down vote













      Try this



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





      share|improve this answer























      • A tweak needed to cover the conversion of : to , in the final column ?
        – steve
        Jun 25 at 19:29






      • 1




        @steve thanks.. updated
        – SivaPrasath
        Jun 25 at 19:31














      up vote
      2
      down vote













      Try this



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





      share|improve this answer























      • A tweak needed to cover the conversion of : to , in the final column ?
        – steve
        Jun 25 at 19:29






      • 1




        @steve thanks.. updated
        – SivaPrasath
        Jun 25 at 19:31












      up vote
      2
      down vote










      up vote
      2
      down vote









      Try this



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





      share|improve this answer















      Try this



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






      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited Jun 25 at 19:54


























      answered Jun 25 at 19:18









      SivaPrasath

      3,88611737




      3,88611737











      • A tweak needed to cover the conversion of : to , in the final column ?
        – steve
        Jun 25 at 19:29






      • 1




        @steve thanks.. updated
        – SivaPrasath
        Jun 25 at 19:31
















      • A tweak needed to cover the conversion of : to , in the final column ?
        – steve
        Jun 25 at 19:29






      • 1




        @steve thanks.. updated
        – SivaPrasath
        Jun 25 at 19:31















      A tweak needed to cover the conversion of : to , in the final column ?
      – steve
      Jun 25 at 19:29




      A tweak needed to cover the conversion of : to , in the final column ?
      – steve
      Jun 25 at 19:29




      1




      1




      @steve thanks.. updated
      – SivaPrasath
      Jun 25 at 19:31




      @steve thanks.. updated
      – SivaPrasath
      Jun 25 at 19:31










      up vote
      1
      down vote













      $ 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


      Where script.awk is



      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





      share|improve this answer

























        up vote
        1
        down vote













        $ 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


        Where script.awk is



        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





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          $ 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


          Where script.awk is



          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





          share|improve this answer













          $ 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


          Where script.awk is



          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






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 25 at 19:40









          Kusalananda

          101k13199312




          101k13199312




















              up vote
              1
              down vote













              perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt


              Output:



              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


              Explanation:



              1. Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array @F. Array is indexed from 0 and goes all the way to $#F. So $#F-1 is the second last element.

              2. All lines except the 2nd are to be processed, in that the last element is the deciding factor.

              3. For the first line, the last element is replaced by multiple field names.

              4. For the remaining, (remember 2nd is already discarded) are split up on : and these split up fields come in place of the last element.

              5. The joining of all the elements is done by the , and then printed to stdout.

              With POSIX sed, we can do the following:



              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





              share|improve this answer

























                up vote
                1
                down vote













                perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt


                Output:



                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


                Explanation:



                1. Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array @F. Array is indexed from 0 and goes all the way to $#F. So $#F-1 is the second last element.

                2. All lines except the 2nd are to be processed, in that the last element is the deciding factor.

                3. For the first line, the last element is replaced by multiple field names.

                4. For the remaining, (remember 2nd is already discarded) are split up on : and these split up fields come in place of the last element.

                5. The joining of all the elements is done by the , and then printed to stdout.

                With POSIX sed, we can do the following:



                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





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt


                  Output:



                  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


                  Explanation:



                  1. Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array @F. Array is indexed from 0 and goes all the way to $#F. So $#F-1 is the second last element.

                  2. All lines except the 2nd are to be processed, in that the last element is the deciding factor.

                  3. For the first line, the last element is replaced by multiple field names.

                  4. For the remaining, (remember 2nd is already discarded) are split up on : and these split up fields come in place of the last element.

                  5. The joining of all the elements is done by the , and then printed to stdout.

                  With POSIX sed, we can do the following:



                  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





                  share|improve this answer













                  perl -lane 'print join ",", @F[0..$#F-1], $. == 1 ? qw/appnbr request tasknbr appctx username/ : split /:/, $F[-1] unless $. == 2' input-file.txt


                  Output:



                  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


                  Explanation:



                  1. Split each line, a.k.a., record, on whitespace(s) and store the reultant fields in the array @F. Array is indexed from 0 and goes all the way to $#F. So $#F-1 is the second last element.

                  2. All lines except the 2nd are to be processed, in that the last element is the deciding factor.

                  3. For the first line, the last element is replaced by multiple field names.

                  4. For the remaining, (remember 2nd is already discarded) are split up on : and these split up fields come in place of the last element.

                  5. The joining of all the elements is done by the , and then printed to stdout.

                  With POSIX sed, we can do the following:



                  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






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 26 at 8:25









                  Rakesh Sharma

                  3433




                  3433




















                      up vote
                      1
                      down vote













                      Here's my awk stab at it.



                      awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt


                      • NR>2 so that record number 1 and 2 (headers) are skipped

                      • print fields $1 thru $5 with commas

                      • instead of printing field $6, print it with : replaced with ","





                      share|improve this answer























                      • How would I do the new headers aspect?
                        – fball4life36
                        Jun 27 at 19:44










                      • now added the headers part.
                        – steve
                        Jun 27 at 20:25










                      • The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
                        – fball4life36
                        Jun 27 at 21:04














                      up vote
                      1
                      down vote













                      Here's my awk stab at it.



                      awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt


                      • NR>2 so that record number 1 and 2 (headers) are skipped

                      • print fields $1 thru $5 with commas

                      • instead of printing field $6, print it with : replaced with ","





                      share|improve this answer























                      • How would I do the new headers aspect?
                        – fball4life36
                        Jun 27 at 19:44










                      • now added the headers part.
                        – steve
                        Jun 27 at 20:25










                      • The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
                        – fball4life36
                        Jun 27 at 21:04












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      Here's my awk stab at it.



                      awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt


                      • NR>2 so that record number 1 and 2 (headers) are skipped

                      • print fields $1 thru $5 with commas

                      • instead of printing field $6, print it with : replaced with ","





                      share|improve this answer















                      Here's my awk stab at it.



                      awk 'BEGINprint "id,name,tid,mod,state,appnbr,request,tasknbr,appctx,username"NR>2print $1","$2","$3","$4","$5","gensub(/:/,",","g",$6)' file.txt


                      • NR>2 so that record number 1 and 2 (headers) are skipped

                      • print fields $1 thru $5 with commas

                      • instead of printing field $6, print it with : replaced with ","






                      share|improve this answer















                      share|improve this answer



                      share|improve this answer








                      edited Jun 27 at 20:24


























                      answered Jun 25 at 19:41









                      steve

                      12.1k22047




                      12.1k22047











                      • How would I do the new headers aspect?
                        – fball4life36
                        Jun 27 at 19:44










                      • now added the headers part.
                        – steve
                        Jun 27 at 20:25










                      • The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
                        – fball4life36
                        Jun 27 at 21:04
















                      • How would I do the new headers aspect?
                        – fball4life36
                        Jun 27 at 19:44










                      • now added the headers part.
                        – steve
                        Jun 27 at 20:25










                      • The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
                        – fball4life36
                        Jun 27 at 21:04















                      How would I do the new headers aspect?
                      – fball4life36
                      Jun 27 at 19:44




                      How would I do the new headers aspect?
                      – fball4life36
                      Jun 27 at 19:44












                      now added the headers part.
                      – steve
                      Jun 27 at 20:25




                      now added the headers part.
                      – steve
                      Jun 27 at 20:25












                      The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
                      – fball4life36
                      Jun 27 at 21:04




                      The previous second line (-------------------------------------------------------------------------------- ) reappear. I also would like to append those columns name to the same line pid,name,tid,mod.
                      – fball4life36
                      Jun 27 at 21:04












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451825%2fkorn-bash-shell-how-to-do-i-convert-the-contents-to-the-below-format%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?