Converting data and using sed/awk











up vote
-1
down vote

favorite












I have a file which is provided below, Col1/Field 1 will always have duplicate data the rest of the fields may/may not but am not worried of other columns, all I need is if there is duplicate data it need to be printed with empty space for col1 and the values of Field 1 will be ordered so no values repeats further down the rows.



 COL1  | COL2 | COL3
----------------------
A1 | 98 | P
A1 | 98 | P
A1 | 98 | P
B1 | 98 | P
B1 | 98 | P
B1 | 98 | P
C1 | 98 | P
C1 | 98 | P
C1 | 98 | P


need to convert and the awk/sed need to be applied on col1 only
(the output should look like below after awk/sed/cut is used)



 COL1  | COL2 | COL3
----------------------
A1 | 98 | P
| 98 | P
| 98 | P
B1 | 98 | P
| 98 | P
| 98 | P
C1 | 98 | P
| 98 | P
| 98 | P


awk '!x[$1]++' file <-- is removing whole line



awk/sed/cut anything is fine.
Wrote 50 lines of code , struck at last phase










share|improve this question




























    up vote
    -1
    down vote

    favorite












    I have a file which is provided below, Col1/Field 1 will always have duplicate data the rest of the fields may/may not but am not worried of other columns, all I need is if there is duplicate data it need to be printed with empty space for col1 and the values of Field 1 will be ordered so no values repeats further down the rows.



     COL1  | COL2 | COL3
    ----------------------
    A1 | 98 | P
    A1 | 98 | P
    A1 | 98 | P
    B1 | 98 | P
    B1 | 98 | P
    B1 | 98 | P
    C1 | 98 | P
    C1 | 98 | P
    C1 | 98 | P


    need to convert and the awk/sed need to be applied on col1 only
    (the output should look like below after awk/sed/cut is used)



     COL1  | COL2 | COL3
    ----------------------
    A1 | 98 | P
    | 98 | P
    | 98 | P
    B1 | 98 | P
    | 98 | P
    | 98 | P
    C1 | 98 | P
    | 98 | P
    | 98 | P


    awk '!x[$1]++' file <-- is removing whole line



    awk/sed/cut anything is fine.
    Wrote 50 lines of code , struck at last phase










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have a file which is provided below, Col1/Field 1 will always have duplicate data the rest of the fields may/may not but am not worried of other columns, all I need is if there is duplicate data it need to be printed with empty space for col1 and the values of Field 1 will be ordered so no values repeats further down the rows.



       COL1  | COL2 | COL3
      ----------------------
      A1 | 98 | P
      A1 | 98 | P
      A1 | 98 | P
      B1 | 98 | P
      B1 | 98 | P
      B1 | 98 | P
      C1 | 98 | P
      C1 | 98 | P
      C1 | 98 | P


      need to convert and the awk/sed need to be applied on col1 only
      (the output should look like below after awk/sed/cut is used)



       COL1  | COL2 | COL3
      ----------------------
      A1 | 98 | P
      | 98 | P
      | 98 | P
      B1 | 98 | P
      | 98 | P
      | 98 | P
      C1 | 98 | P
      | 98 | P
      | 98 | P


      awk '!x[$1]++' file <-- is removing whole line



      awk/sed/cut anything is fine.
      Wrote 50 lines of code , struck at last phase










      share|improve this question















      I have a file which is provided below, Col1/Field 1 will always have duplicate data the rest of the fields may/may not but am not worried of other columns, all I need is if there is duplicate data it need to be printed with empty space for col1 and the values of Field 1 will be ordered so no values repeats further down the rows.



       COL1  | COL2 | COL3
      ----------------------
      A1 | 98 | P
      A1 | 98 | P
      A1 | 98 | P
      B1 | 98 | P
      B1 | 98 | P
      B1 | 98 | P
      C1 | 98 | P
      C1 | 98 | P
      C1 | 98 | P


      need to convert and the awk/sed need to be applied on col1 only
      (the output should look like below after awk/sed/cut is used)



       COL1  | COL2 | COL3
      ----------------------
      A1 | 98 | P
      | 98 | P
      | 98 | P
      B1 | 98 | P
      | 98 | P
      | 98 | P
      C1 | 98 | P
      | 98 | P
      | 98 | P


      awk '!x[$1]++' file <-- is removing whole line



      awk/sed/cut anything is fine.
      Wrote 50 lines of code , struck at last phase







      shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 at 6:56









      Rui F Ribeiro

      38.3k1476127




      38.3k1476127










      asked Nov 28 at 3:38









      vj56789

      61




      61






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          $ awk -F '|' 'BEGIN { OFS=FS } { c1 = $1 } c1 == prev { gsub(".", " ", $1) } { prev = c1; print }' file
          COL1 | COL2 | COL3
          ----------------------
          A1 | 98 | P
          | 98 | P
          | 98 | P
          B1 | 98 | P
          | 98 | P
          | 98 | P
          C1 | 98 | P
          | 98 | P
          | 98 | P


          The awk program reads |-delimited input data and writes |-delimited output data.



          For each line in the input, it extracts the first column into c1 and compares it to the previous first column, prev. If they are the same, the value of the first column is overwritten by spaces (this is what the gsub() does, and it prevents the column width from changing). The recorded "previous first column" is then updated with the value of c1 and the (possibly modified) line is printed.






          share|improve this answer





















          • First off - Thanks your solution worked.
            – vj56789
            Nov 28 at 15:06










          • What to do if I wish to do the same comparison with Col1 and Col2
            – vj56789
            Nov 28 at 15:07











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          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%2f484570%2fconverting-data-and-using-sed-awk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          $ awk -F '|' 'BEGIN { OFS=FS } { c1 = $1 } c1 == prev { gsub(".", " ", $1) } { prev = c1; print }' file
          COL1 | COL2 | COL3
          ----------------------
          A1 | 98 | P
          | 98 | P
          | 98 | P
          B1 | 98 | P
          | 98 | P
          | 98 | P
          C1 | 98 | P
          | 98 | P
          | 98 | P


          The awk program reads |-delimited input data and writes |-delimited output data.



          For each line in the input, it extracts the first column into c1 and compares it to the previous first column, prev. If they are the same, the value of the first column is overwritten by spaces (this is what the gsub() does, and it prevents the column width from changing). The recorded "previous first column" is then updated with the value of c1 and the (possibly modified) line is printed.






          share|improve this answer





















          • First off - Thanks your solution worked.
            – vj56789
            Nov 28 at 15:06










          • What to do if I wish to do the same comparison with Col1 and Col2
            – vj56789
            Nov 28 at 15:07















          up vote
          2
          down vote













          $ awk -F '|' 'BEGIN { OFS=FS } { c1 = $1 } c1 == prev { gsub(".", " ", $1) } { prev = c1; print }' file
          COL1 | COL2 | COL3
          ----------------------
          A1 | 98 | P
          | 98 | P
          | 98 | P
          B1 | 98 | P
          | 98 | P
          | 98 | P
          C1 | 98 | P
          | 98 | P
          | 98 | P


          The awk program reads |-delimited input data and writes |-delimited output data.



          For each line in the input, it extracts the first column into c1 and compares it to the previous first column, prev. If they are the same, the value of the first column is overwritten by spaces (this is what the gsub() does, and it prevents the column width from changing). The recorded "previous first column" is then updated with the value of c1 and the (possibly modified) line is printed.






          share|improve this answer





















          • First off - Thanks your solution worked.
            – vj56789
            Nov 28 at 15:06










          • What to do if I wish to do the same comparison with Col1 and Col2
            – vj56789
            Nov 28 at 15:07













          up vote
          2
          down vote










          up vote
          2
          down vote









          $ awk -F '|' 'BEGIN { OFS=FS } { c1 = $1 } c1 == prev { gsub(".", " ", $1) } { prev = c1; print }' file
          COL1 | COL2 | COL3
          ----------------------
          A1 | 98 | P
          | 98 | P
          | 98 | P
          B1 | 98 | P
          | 98 | P
          | 98 | P
          C1 | 98 | P
          | 98 | P
          | 98 | P


          The awk program reads |-delimited input data and writes |-delimited output data.



          For each line in the input, it extracts the first column into c1 and compares it to the previous first column, prev. If they are the same, the value of the first column is overwritten by spaces (this is what the gsub() does, and it prevents the column width from changing). The recorded "previous first column" is then updated with the value of c1 and the (possibly modified) line is printed.






          share|improve this answer












          $ awk -F '|' 'BEGIN { OFS=FS } { c1 = $1 } c1 == prev { gsub(".", " ", $1) } { prev = c1; print }' file
          COL1 | COL2 | COL3
          ----------------------
          A1 | 98 | P
          | 98 | P
          | 98 | P
          B1 | 98 | P
          | 98 | P
          | 98 | P
          C1 | 98 | P
          | 98 | P
          | 98 | P


          The awk program reads |-delimited input data and writes |-delimited output data.



          For each line in the input, it extracts the first column into c1 and compares it to the previous first column, prev. If they are the same, the value of the first column is overwritten by spaces (this is what the gsub() does, and it prevents the column width from changing). The recorded "previous first column" is then updated with the value of c1 and the (possibly modified) line is printed.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 at 7:29









          Kusalananda

          118k16223361




          118k16223361












          • First off - Thanks your solution worked.
            – vj56789
            Nov 28 at 15:06










          • What to do if I wish to do the same comparison with Col1 and Col2
            – vj56789
            Nov 28 at 15:07


















          • First off - Thanks your solution worked.
            – vj56789
            Nov 28 at 15:06










          • What to do if I wish to do the same comparison with Col1 and Col2
            – vj56789
            Nov 28 at 15:07
















          First off - Thanks your solution worked.
          – vj56789
          Nov 28 at 15:06




          First off - Thanks your solution worked.
          – vj56789
          Nov 28 at 15:06












          What to do if I wish to do the same comparison with Col1 and Col2
          – vj56789
          Nov 28 at 15:07




          What to do if I wish to do the same comparison with Col1 and Col2
          – vj56789
          Nov 28 at 15:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484570%2fconverting-data-and-using-sed-awk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          サソリ

          広島県道265号伴広島線

          Setup Asymptote in Texstudio