Print only the multiple first characters

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












0















I have a big text file I want to print only the first 4 and the first 5 and the first 8 characters of each line in one command line.



For example
I have the lines:



123456789ab
ABCdefgih55


So the output have to be:



1234
ABCd
12345
ABCde
12345678
ABCdefgh









share|improve this question




























    0















    I have a big text file I want to print only the first 4 and the first 5 and the first 8 characters of each line in one command line.



    For example
    I have the lines:



    123456789ab
    ABCdefgih55


    So the output have to be:



    1234
    ABCd
    12345
    ABCde
    12345678
    ABCdefgh









    share|improve this question


























      0












      0








      0








      I have a big text file I want to print only the first 4 and the first 5 and the first 8 characters of each line in one command line.



      For example
      I have the lines:



      123456789ab
      ABCdefgih55


      So the output have to be:



      1234
      ABCd
      12345
      ABCde
      12345678
      ABCdefgh









      share|improve this question
















      I have a big text file I want to print only the first 4 and the first 5 and the first 8 characters of each line in one command line.



      For example
      I have the lines:



      123456789ab
      ABCdefgih55


      So the output have to be:



      1234
      ABCd
      12345
      ABCde
      12345678
      ABCdefgh






      text-processing sed command-line cut






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 6 at 8:20









      Kusalananda

      133k17253416




      133k17253416










      asked Feb 6 at 7:56









      AhmedAhmed

      615




      615




















          3 Answers
          3






          active

          oldest

          votes


















          3














          for len in 4 5 8; do
          cut -c "1-$len" file
          done


          This uses cut -c repeatedly to cut out the first part of each line of the file called file. The length of the cut out bit is depending on the loop variable len.



          If you're strict about that "one line" criteria:



          for len in 4 5 8; do cut -c "1-$len" file; done


          Or, as an easy to use shell function:



          cut_to_lengths () 
          file=$1; shift
          for len do
          cut -c "1-$len" "$file"
          done



          Using it:



          $ cut_to_lengths file 4 5 8 1
          1234
          ABCd
          12345
          ABCde
          12345678
          ABCdefgi
          1
          A



          In comments you specify that you don't want to output lines if they are shorter the cut length.



          To do this, we can change the cut command into an awk command:



          awk -v len="$len" 'length >= len print substr($0, 1, len) '


          Replace the cut -c "1-$len" with the above awk command in the code above.






          share|improve this answer

























          • if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:34











          • @Ahmed See update.

            – Kusalananda
            Feb 6 at 11:41


















          0














          Your example is not well formated. I guess the whitespace should be a new line? If so the simplest way is to use cut



          $ cut -c -4 input will print the first 4 character of each line. Repeat it with any number of characters you like.






          share|improve this answer























          • Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

            – Ahmed
            Feb 6 at 8:16


















          0














          try the following



          for line in `cat input`; do echo $line | cut -c 1-4 && echo $line | cut -c 1-5 && echo $line | cut -c 1-8; done


          this gives



          1234
          12345
          12345678
          ABCd
          ABCde
          ABCdefgi





          share|improve this answer























          • The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

            – Kusalananda
            Feb 6 at 8:21











          • More info here.

            – Sparhawk
            Feb 6 at 8:31











          • @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:20











          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',
          autoActivateHeartbeat: false,
          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%2f498986%2fprint-only-the-multiple-first-characters%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          for len in 4 5 8; do
          cut -c "1-$len" file
          done


          This uses cut -c repeatedly to cut out the first part of each line of the file called file. The length of the cut out bit is depending on the loop variable len.



          If you're strict about that "one line" criteria:



          for len in 4 5 8; do cut -c "1-$len" file; done


          Or, as an easy to use shell function:



          cut_to_lengths () 
          file=$1; shift
          for len do
          cut -c "1-$len" "$file"
          done



          Using it:



          $ cut_to_lengths file 4 5 8 1
          1234
          ABCd
          12345
          ABCde
          12345678
          ABCdefgi
          1
          A



          In comments you specify that you don't want to output lines if they are shorter the cut length.



          To do this, we can change the cut command into an awk command:



          awk -v len="$len" 'length >= len print substr($0, 1, len) '


          Replace the cut -c "1-$len" with the above awk command in the code above.






          share|improve this answer

























          • if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:34











          • @Ahmed See update.

            – Kusalananda
            Feb 6 at 11:41















          3














          for len in 4 5 8; do
          cut -c "1-$len" file
          done


          This uses cut -c repeatedly to cut out the first part of each line of the file called file. The length of the cut out bit is depending on the loop variable len.



          If you're strict about that "one line" criteria:



          for len in 4 5 8; do cut -c "1-$len" file; done


          Or, as an easy to use shell function:



          cut_to_lengths () 
          file=$1; shift
          for len do
          cut -c "1-$len" "$file"
          done



          Using it:



          $ cut_to_lengths file 4 5 8 1
          1234
          ABCd
          12345
          ABCde
          12345678
          ABCdefgi
          1
          A



          In comments you specify that you don't want to output lines if they are shorter the cut length.



          To do this, we can change the cut command into an awk command:



          awk -v len="$len" 'length >= len print substr($0, 1, len) '


          Replace the cut -c "1-$len" with the above awk command in the code above.






          share|improve this answer

























          • if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:34











          • @Ahmed See update.

            – Kusalananda
            Feb 6 at 11:41













          3












          3








          3







          for len in 4 5 8; do
          cut -c "1-$len" file
          done


          This uses cut -c repeatedly to cut out the first part of each line of the file called file. The length of the cut out bit is depending on the loop variable len.



          If you're strict about that "one line" criteria:



          for len in 4 5 8; do cut -c "1-$len" file; done


          Or, as an easy to use shell function:



          cut_to_lengths () 
          file=$1; shift
          for len do
          cut -c "1-$len" "$file"
          done



          Using it:



          $ cut_to_lengths file 4 5 8 1
          1234
          ABCd
          12345
          ABCde
          12345678
          ABCdefgi
          1
          A



          In comments you specify that you don't want to output lines if they are shorter the cut length.



          To do this, we can change the cut command into an awk command:



          awk -v len="$len" 'length >= len print substr($0, 1, len) '


          Replace the cut -c "1-$len" with the above awk command in the code above.






          share|improve this answer















          for len in 4 5 8; do
          cut -c "1-$len" file
          done


          This uses cut -c repeatedly to cut out the first part of each line of the file called file. The length of the cut out bit is depending on the loop variable len.



          If you're strict about that "one line" criteria:



          for len in 4 5 8; do cut -c "1-$len" file; done


          Or, as an easy to use shell function:



          cut_to_lengths () 
          file=$1; shift
          for len do
          cut -c "1-$len" "$file"
          done



          Using it:



          $ cut_to_lengths file 4 5 8 1
          1234
          ABCd
          12345
          ABCde
          12345678
          ABCdefgi
          1
          A



          In comments you specify that you don't want to output lines if they are shorter the cut length.



          To do this, we can change the cut command into an awk command:



          awk -v len="$len" 'length >= len print substr($0, 1, len) '


          Replace the cut -c "1-$len" with the above awk command in the code above.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 6 at 23:33

























          answered Feb 6 at 8:23









          KusalanandaKusalananda

          133k17253416




          133k17253416












          • if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:34











          • @Ahmed See update.

            – Kusalananda
            Feb 6 at 11:41

















          • if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:34











          • @Ahmed See update.

            – Kusalananda
            Feb 6 at 11:41
















          if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

          – Ahmed
          Feb 6 at 11:34





          if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

          – Ahmed
          Feb 6 at 11:34













          @Ahmed See update.

          – Kusalananda
          Feb 6 at 11:41





          @Ahmed See update.

          – Kusalananda
          Feb 6 at 11:41













          0














          Your example is not well formated. I guess the whitespace should be a new line? If so the simplest way is to use cut



          $ cut -c -4 input will print the first 4 character of each line. Repeat it with any number of characters you like.






          share|improve this answer























          • Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

            – Ahmed
            Feb 6 at 8:16















          0














          Your example is not well formated. I guess the whitespace should be a new line? If so the simplest way is to use cut



          $ cut -c -4 input will print the first 4 character of each line. Repeat it with any number of characters you like.






          share|improve this answer























          • Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

            – Ahmed
            Feb 6 at 8:16













          0












          0








          0







          Your example is not well formated. I guess the whitespace should be a new line? If so the simplest way is to use cut



          $ cut -c -4 input will print the first 4 character of each line. Repeat it with any number of characters you like.






          share|improve this answer













          Your example is not well formated. I guess the whitespace should be a new line? If so the simplest way is to use cut



          $ cut -c -4 input will print the first 4 character of each line. Repeat it with any number of characters you like.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 6 at 8:11









          finswimmerfinswimmer

          71717




          71717












          • Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

            – Ahmed
            Feb 6 at 8:16

















          • Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

            – Ahmed
            Feb 6 at 8:16
















          Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

          – Ahmed
          Feb 6 at 8:16





          Dear finswimmer i forgot to mention that i tried this command cut -c -4 input but i want to do this in one command line for the all the first 4 and 5 and 8 characters.

          – Ahmed
          Feb 6 at 8:16











          0














          try the following



          for line in `cat input`; do echo $line | cut -c 1-4 && echo $line | cut -c 1-5 && echo $line | cut -c 1-8; done


          this gives



          1234
          12345
          12345678
          ABCd
          ABCde
          ABCdefgi





          share|improve this answer























          • The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

            – Kusalananda
            Feb 6 at 8:21











          • More info here.

            – Sparhawk
            Feb 6 at 8:31











          • @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:20
















          0














          try the following



          for line in `cat input`; do echo $line | cut -c 1-4 && echo $line | cut -c 1-5 && echo $line | cut -c 1-8; done


          this gives



          1234
          12345
          12345678
          ABCd
          ABCde
          ABCdefgi





          share|improve this answer























          • The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

            – Kusalananda
            Feb 6 at 8:21











          • More info here.

            – Sparhawk
            Feb 6 at 8:31











          • @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:20














          0












          0








          0







          try the following



          for line in `cat input`; do echo $line | cut -c 1-4 && echo $line | cut -c 1-5 && echo $line | cut -c 1-8; done


          this gives



          1234
          12345
          12345678
          ABCd
          ABCde
          ABCdefgi





          share|improve this answer













          try the following



          for line in `cat input`; do echo $line | cut -c 1-4 && echo $line | cut -c 1-5 && echo $line | cut -c 1-8; done


          this gives



          1234
          12345
          12345678
          ABCd
          ABCde
          ABCdefgi






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 6 at 8:12









          Zaid AmirehZaid Amireh

          11




          11












          • The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

            – Kusalananda
            Feb 6 at 8:21











          • More info here.

            – Sparhawk
            Feb 6 at 8:31











          • @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:20


















          • The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

            – Kusalananda
            Feb 6 at 8:21











          • More info here.

            – Sparhawk
            Feb 6 at 8:31











          • @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

            – Ahmed
            Feb 6 at 11:20

















          The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

          – Kusalananda
          Feb 6 at 8:21





          The output does not seem to match the expected output. Also, if any line contains filename globbing characters or spaces, your code wouldn't handle it properly.

          – Kusalananda
          Feb 6 at 8:21













          More info here.

          – Sparhawk
          Feb 6 at 8:31





          More info here.

          – Sparhawk
          Feb 6 at 8:31













          @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

          – Ahmed
          Feb 6 at 11:20






          @Zaid Amireh if i have lines contain less than 4 characters so i don't want them to be appeared in the output. If i have in the input a line contains one character for example: 1 so it will be eapeated in the out put how to prevent the lines that contain less than 4 characters from being appeared?

          – Ahmed
          Feb 6 at 11:20


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498986%2fprint-only-the-multiple-first-characters%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

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)