Modify a string to remove characters

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











up vote
0
down vote

favorite












I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.



For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column



I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column







share|improve this question























    up vote
    0
    down vote

    favorite












    I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
    The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.



    For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column



    I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
      The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.



      For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column



      I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column







      share|improve this question











      I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
      The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.



      For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column



      I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 7 at 13:52









      Meghan

      1




      1




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Using awk:



          awk 'print substr($18,1,12)' input


          This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.



          For CSV:



          awk -F, 'print substr($18,1,12)' input


          Based on Steeldriver's comment for adding this output to a new column:



          awk '$(NF+1) = substr($18,1,12) 1' input > output


          Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.






          share|improve this answer























          • Thanks, is there a way to print the trimmed down ID in a new column within the original file?
            – Meghan
            Jun 7 at 14:07










          • I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
            – Meghan
            Jun 7 at 14:25











          • Getting it to print the what?
            – Jesse_b
            Jun 7 at 14:27










          • its returning data from the 12 to last column
            – Meghan
            Jun 7 at 14:28






          • 1




            Please add the sample data as an edit to your question in a code block so that it's legible.
            – Jesse_b
            Jun 7 at 14:50










          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%2f448433%2fmodify-a-string-to-remove-characters%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          Using awk:



          awk 'print substr($18,1,12)' input


          This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.



          For CSV:



          awk -F, 'print substr($18,1,12)' input


          Based on Steeldriver's comment for adding this output to a new column:



          awk '$(NF+1) = substr($18,1,12) 1' input > output


          Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.






          share|improve this answer























          • Thanks, is there a way to print the trimmed down ID in a new column within the original file?
            – Meghan
            Jun 7 at 14:07










          • I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
            – Meghan
            Jun 7 at 14:25











          • Getting it to print the what?
            – Jesse_b
            Jun 7 at 14:27










          • its returning data from the 12 to last column
            – Meghan
            Jun 7 at 14:28






          • 1




            Please add the sample data as an edit to your question in a code block so that it's legible.
            – Jesse_b
            Jun 7 at 14:50














          up vote
          3
          down vote













          Using awk:



          awk 'print substr($18,1,12)' input


          This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.



          For CSV:



          awk -F, 'print substr($18,1,12)' input


          Based on Steeldriver's comment for adding this output to a new column:



          awk '$(NF+1) = substr($18,1,12) 1' input > output


          Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.






          share|improve this answer























          • Thanks, is there a way to print the trimmed down ID in a new column within the original file?
            – Meghan
            Jun 7 at 14:07










          • I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
            – Meghan
            Jun 7 at 14:25











          • Getting it to print the what?
            – Jesse_b
            Jun 7 at 14:27










          • its returning data from the 12 to last column
            – Meghan
            Jun 7 at 14:28






          • 1




            Please add the sample data as an edit to your question in a code block so that it's legible.
            – Jesse_b
            Jun 7 at 14:50












          up vote
          3
          down vote










          up vote
          3
          down vote









          Using awk:



          awk 'print substr($18,1,12)' input


          This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.



          For CSV:



          awk -F, 'print substr($18,1,12)' input


          Based on Steeldriver's comment for adding this output to a new column:



          awk '$(NF+1) = substr($18,1,12) 1' input > output


          Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.






          share|improve this answer















          Using awk:



          awk 'print substr($18,1,12)' input


          This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.



          For CSV:



          awk -F, 'print substr($18,1,12)' input


          Based on Steeldriver's comment for adding this output to a new column:



          awk '$(NF+1) = substr($18,1,12) 1' input > output


          Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 7 at 14:10


























          answered Jun 7 at 13:58









          Jesse_b

          10.2k22658




          10.2k22658











          • Thanks, is there a way to print the trimmed down ID in a new column within the original file?
            – Meghan
            Jun 7 at 14:07










          • I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
            – Meghan
            Jun 7 at 14:25











          • Getting it to print the what?
            – Jesse_b
            Jun 7 at 14:27










          • its returning data from the 12 to last column
            – Meghan
            Jun 7 at 14:28






          • 1




            Please add the sample data as an edit to your question in a code block so that it's legible.
            – Jesse_b
            Jun 7 at 14:50
















          • Thanks, is there a way to print the trimmed down ID in a new column within the original file?
            – Meghan
            Jun 7 at 14:07










          • I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
            – Meghan
            Jun 7 at 14:25











          • Getting it to print the what?
            – Jesse_b
            Jun 7 at 14:27










          • its returning data from the 12 to last column
            – Meghan
            Jun 7 at 14:28






          • 1




            Please add the sample data as an edit to your question in a code block so that it's legible.
            – Jesse_b
            Jun 7 at 14:50















          Thanks, is there a way to print the trimmed down ID in a new column within the original file?
          – Meghan
          Jun 7 at 14:07




          Thanks, is there a way to print the trimmed down ID in a new column within the original file?
          – Meghan
          Jun 7 at 14:07












          I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
          – Meghan
          Jun 7 at 14:25





          I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
          – Meghan
          Jun 7 at 14:25













          Getting it to print the what?
          – Jesse_b
          Jun 7 at 14:27




          Getting it to print the what?
          – Jesse_b
          Jun 7 at 14:27












          its returning data from the 12 to last column
          – Meghan
          Jun 7 at 14:28




          its returning data from the 12 to last column
          – Meghan
          Jun 7 at 14:28




          1




          1




          Please add the sample data as an edit to your question in a code block so that it's legible.
          – Jesse_b
          Jun 7 at 14:50




          Please add the sample data as an edit to your question in a code block so that it's legible.
          – Jesse_b
          Jun 7 at 14:50












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448433%2fmodify-a-string-to-remove-characters%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)