find string and print first and last characters of line











up vote
1
down vote

favorite












I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.



For example, cat file1.txt



1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line


And my output would be:



1234567 x 
3456789 y
5678901 z









share|improve this question









New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Expected output seems wrong .... second line starting with 2345678 is irrelevant.
    – George Vasiliou
    Dec 1 at 21:36






  • 1




    ... as is the third.
    – RudiC
    Dec 1 at 21:45










  • thanks, typo on my part. fixed now.
    – user2535719
    Dec 1 at 21:56















up vote
1
down vote

favorite












I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.



For example, cat file1.txt



1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line


And my output would be:



1234567 x 
3456789 y
5678901 z









share|improve this question









New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Expected output seems wrong .... second line starting with 2345678 is irrelevant.
    – George Vasiliou
    Dec 1 at 21:36






  • 1




    ... as is the third.
    – RudiC
    Dec 1 at 21:45










  • thanks, typo on my part. fixed now.
    – user2535719
    Dec 1 at 21:56













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.



For example, cat file1.txt



1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line


And my output would be:



1234567 x 
3456789 y
5678901 z









share|improve this question









New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character.



For example, cat file1.txt



1234567 New line with irrelevant info x end line
2345678 irrelevant line
3456789 New line with different irrelevant info y end line
4567890 irrelevant line
5678901 New line with yet more irrelevant info z end line


And my output would be:



1234567 x 
3456789 y
5678901 z






sed grep cut






share|improve this question









New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 1 at 21:55





















New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 1 at 21:09









user2535719

62




62




New contributor




user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user2535719 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Expected output seems wrong .... second line starting with 2345678 is irrelevant.
    – George Vasiliou
    Dec 1 at 21:36






  • 1




    ... as is the third.
    – RudiC
    Dec 1 at 21:45










  • thanks, typo on my part. fixed now.
    – user2535719
    Dec 1 at 21:56














  • 1




    Expected output seems wrong .... second line starting with 2345678 is irrelevant.
    – George Vasiliou
    Dec 1 at 21:36






  • 1




    ... as is the third.
    – RudiC
    Dec 1 at 21:45










  • thanks, typo on my part. fixed now.
    – user2535719
    Dec 1 at 21:56








1




1




Expected output seems wrong .... second line starting with 2345678 is irrelevant.
– George Vasiliou
Dec 1 at 21:36




Expected output seems wrong .... second line starting with 2345678 is irrelevant.
– George Vasiliou
Dec 1 at 21:36




1




1




... as is the third.
– RudiC
Dec 1 at 21:45




... as is the third.
– RudiC
Dec 1 at 21:45












thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56




thanks, typo on my part. fixed now.
– user2535719
Dec 1 at 21:56










3 Answers
3






active

oldest

votes

















up vote
1
down vote













Choose one you like:



awk solution:



awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt




sed solution:



sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt




Sample output (for both approaches):



1234567 x
3456789 y
5678901 z





share|improve this answer






























    up vote
    1
    down vote













    POSIXly:



    Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr(), you can add a && length >= 10 or && length >= 17 after /New/ to skip the lines that have fewer than 10 or 17 characters):



    awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'


    or assuming the lines contain at least 17 characters (the lines that don't will be skipped):



    sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'





    share|improve this answer

















    • 1




      What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
      – George Vasiliou
      Dec 1 at 21:53










    • @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
      – Stéphane Chazelas
      Dec 1 at 21:56












    • ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
      – George Vasiliou
      Dec 1 at 22:00




















    up vote
    0
    down vote













    This is one "brutal" gawk solution that does the job, using null as field separator FS and output field separator OFS , meaning each char of inputfile is considered to be a field for awk.



    awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
    1234567 x
    3456789 y
    5678901 z


    More solutions with grep / sed will follow.






    share|improve this answer





















      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
      });


      }
      });






      user2535719 is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485398%2ffind-string-and-print-first-and-last-characters-of-line%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








      up vote
      1
      down vote













      Choose one you like:



      awk solution:



      awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt




      sed solution:



      sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt




      Sample output (for both approaches):



      1234567 x
      3456789 y
      5678901 z





      share|improve this answer



























        up vote
        1
        down vote













        Choose one you like:



        awk solution:



        awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt




        sed solution:



        sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt




        Sample output (for both approaches):



        1234567 x
        3456789 y
        5678901 z





        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          Choose one you like:



          awk solution:



          awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt




          sed solution:



          sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt




          Sample output (for both approaches):



          1234567 x
          3456789 y
          5678901 z





          share|improve this answer














          Choose one you like:



          awk solution:



          awk '/New/{ print substr($0, 1, 7), substr($0, length-9, 1) }' file1.txt




          sed solution:



          sed -rn '/New/ s/^(.{7}).*(.).{9}$/1 2/p' file1.txt




          Sample output (for both approaches):



          1234567 x
          3456789 y
          5678901 z






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 1 at 21:46

























          answered Dec 1 at 21:37









          RomanPerekhrest

          22.7k12246




          22.7k12246
























              up vote
              1
              down vote













              POSIXly:



              Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr(), you can add a && length >= 10 or && length >= 17 after /New/ to skip the lines that have fewer than 10 or 17 characters):



              awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'


              or assuming the lines contain at least 17 characters (the lines that don't will be skipped):



              sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'





              share|improve this answer

















              • 1




                What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
                – George Vasiliou
                Dec 1 at 21:53










              • @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
                – Stéphane Chazelas
                Dec 1 at 21:56












              • ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
                – George Vasiliou
                Dec 1 at 22:00

















              up vote
              1
              down vote













              POSIXly:



              Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr(), you can add a && length >= 10 or && length >= 17 after /New/ to skip the lines that have fewer than 10 or 17 characters):



              awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'


              or assuming the lines contain at least 17 characters (the lines that don't will be skipped):



              sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'





              share|improve this answer

















              • 1




                What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
                – George Vasiliou
                Dec 1 at 21:53










              • @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
                – Stéphane Chazelas
                Dec 1 at 21:56












              • ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
                – George Vasiliou
                Dec 1 at 22:00















              up vote
              1
              down vote










              up vote
              1
              down vote









              POSIXly:



              Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr(), you can add a && length >= 10 or && length >= 17 after /New/ to skip the lines that have fewer than 10 or 17 characters):



              awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'


              or assuming the lines contain at least 17 characters (the lines that don't will be skipped):



              sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'





              share|improve this answer












              POSIXly:



              Assuming the lines contain at least 10 characters (if not, the behaviour is unspecified for the second substr(), you can add a && length >= 10 or && length >= 17 after /New/ to skip the lines that have fewer than 10 or 17 characters):



              awk '/New/ {print substr($0, 1, 7), substr($0, length - 9, 1)}'


              or assuming the lines contain at least 17 characters (the lines that don't will be skipped):



              sed -n '/New/ s/^(.{7}).*(.).{9}$/1 2/p'






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 1 at 21:48









              Stéphane Chazelas

              296k54559904




              296k54559904








              • 1




                What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
                – George Vasiliou
                Dec 1 at 21:53










              • @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
                – Stéphane Chazelas
                Dec 1 at 21:56












              • ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
                – George Vasiliou
                Dec 1 at 22:00
















              • 1




                What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
                – George Vasiliou
                Dec 1 at 21:53










              • @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
                – Stéphane Chazelas
                Dec 1 at 21:56












              • ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
                – George Vasiliou
                Dec 1 at 22:00










              1




              1




              What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
              – George Vasiliou
              Dec 1 at 21:53




              What is the difference with the answer of RomanPerekhrest ? Your awk solution is identical and your sed solution is also identical, it just uses basic regex instead of extended regex....
              – George Vasiliou
              Dec 1 at 21:53












              @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
              – Stéphane Chazelas
              Dec 1 at 21:56






              @GeorgeVasiliou, he edited the awk solution in after I had started writing mine and I hadn't seen his edit before I posted mine. His sed one is GNU specific, my intention was to offer a standard solution.
              – Stéphane Chazelas
              Dec 1 at 21:56














              ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
              – George Vasiliou
              Dec 1 at 22:00






              ok then. Fair answer. Has happened also to me. If i had more time my answer would also be like Roman's answer, but this guy was fast! :-)
              – George Vasiliou
              Dec 1 at 22:00












              up vote
              0
              down vote













              This is one "brutal" gawk solution that does the job, using null as field separator FS and output field separator OFS , meaning each char of inputfile is considered to be a field for awk.



              awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
              1234567 x
              3456789 y
              5678901 z


              More solutions with grep / sed will follow.






              share|improve this answer

























                up vote
                0
                down vote













                This is one "brutal" gawk solution that does the job, using null as field separator FS and output field separator OFS , meaning each char of inputfile is considered to be a field for awk.



                awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
                1234567 x
                3456789 y
                5678901 z


                More solutions with grep / sed will follow.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This is one "brutal" gawk solution that does the job, using null as field separator FS and output field separator OFS , meaning each char of inputfile is considered to be a field for awk.



                  awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
                  1234567 x
                  3456789 y
                  5678901 z


                  More solutions with grep / sed will follow.






                  share|improve this answer












                  This is one "brutal" gawk solution that does the job, using null as field separator FS and output field separator OFS , meaning each char of inputfile is considered to be a field for awk.



                  awk '/New/{print $1,$2,$3,$4,$5,$6,$7," ",$(NF-9)}' FS="" OFS="" file1
                  1234567 x
                  3456789 y
                  5678901 z


                  More solutions with grep / sed will follow.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 1 at 21:33









                  George Vasiliou

                  5,57531028




                  5,57531028






















                      user2535719 is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      user2535719 is a new contributor. Be nice, and check out our Code of Conduct.













                      user2535719 is a new contributor. Be nice, and check out our Code of Conduct.












                      user2535719 is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f485398%2ffind-string-and-print-first-and-last-characters-of-line%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

                      數位音樂下載

                      When can things happen in Etherscan, such as the picture below?

                      格利澤436b