highlight a word without affecting the structure of text












1















$cat contents.txt

cat-1.15

cat-1.15

cat-1.15
cat-1.18


The above output has blank lines



$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines



cat-1.15
cat-1.15
cat-1.15
cat-1.18


How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?










share|improve this question




















  • 1





    Possible duplicate of Convince grep to output all lines, not just those with matches

    – steeldriver
    1 hour ago
















1















$cat contents.txt

cat-1.15

cat-1.15

cat-1.15
cat-1.18


The above output has blank lines



$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines



cat-1.15
cat-1.15
cat-1.15
cat-1.18


How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?










share|improve this question




















  • 1





    Possible duplicate of Convince grep to output all lines, not just those with matches

    – steeldriver
    1 hour ago














1












1








1








$cat contents.txt

cat-1.15

cat-1.15

cat-1.15
cat-1.18


The above output has blank lines



$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines



cat-1.15
cat-1.15
cat-1.15
cat-1.18


How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?










share|improve this question
















$cat contents.txt

cat-1.15

cat-1.15

cat-1.15
cat-1.18


The above output has blank lines



$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines



cat-1.15
cat-1.15
cat-1.15
cat-1.18


How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?







awk sed grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







user607694

















asked 3 hours ago









user607694user607694

354




354








  • 1





    Possible duplicate of Convince grep to output all lines, not just those with matches

    – steeldriver
    1 hour ago














  • 1





    Possible duplicate of Convince grep to output all lines, not just those with matches

    – steeldriver
    1 hour ago








1




1





Possible duplicate of Convince grep to output all lines, not just those with matches

– steeldriver
1 hour ago





Possible duplicate of Convince grep to output all lines, not just those with matches

– steeldriver
1 hour ago










3 Answers
3






active

oldest

votes


















4














With GNU grep this can be accomplished with the -z option.




-z, --null-data




Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.





Also this is a UUOC. You can specify an input file with grep.



$ grep --color cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
$ grep --color -z cat contents.txt
cat-1.15

cat-1.15

cat-1.15
cat-1.18





share|improve this answer



















  • 1





    Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

    – Isaac
    17 mins ago



















2














Another possible solution is to provide two patterns to grep: one will be the actual pattern or term to be searched for, and the second one will be an empty string.



$ grep --color -e 'cat' -e '' testfile.txt
cat-1.15

cat-1.15

cat-1.15
cat-1.18


The -e option is used for specify multiple patterns. From the manual:




-e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a
pattern beginning with a hyphen (-). (-e is specified by POSIX.)




You can also combine those patterns as a single extended regular expression, if required:



$ grep --color -E 'cat|' testfile.txt
cat-1.15

cat-1.15

cat-1.15
cat-1.18


Also, you can simply add another pattern to the list if you need to highlight more than one keyword.






share|improve this answer































    0














    I tried with below command



    sed -r "/cat/{N;s/^$/&=/}" filename


    awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename





    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',
      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%2f496717%2fhighlight-a-word-without-affecting-the-structure-of-text%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









      4














      With GNU grep this can be accomplished with the -z option.




      -z, --null-data




      Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
      option, this option can be used with commands like sort -z to process arbitrary file names.





      Also this is a UUOC. You can specify an input file with grep.



      $ grep --color cat contents.txt
      cat-1.15
      cat-1.15
      cat-1.15
      cat-1.18
      $ grep --color -z cat contents.txt
      cat-1.15

      cat-1.15

      cat-1.15
      cat-1.18





      share|improve this answer



















      • 1





        Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

        – Isaac
        17 mins ago
















      4














      With GNU grep this can be accomplished with the -z option.




      -z, --null-data




      Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
      option, this option can be used with commands like sort -z to process arbitrary file names.





      Also this is a UUOC. You can specify an input file with grep.



      $ grep --color cat contents.txt
      cat-1.15
      cat-1.15
      cat-1.15
      cat-1.18
      $ grep --color -z cat contents.txt
      cat-1.15

      cat-1.15

      cat-1.15
      cat-1.18





      share|improve this answer



















      • 1





        Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

        – Isaac
        17 mins ago














      4












      4








      4







      With GNU grep this can be accomplished with the -z option.




      -z, --null-data




      Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
      option, this option can be used with commands like sort -z to process arbitrary file names.





      Also this is a UUOC. You can specify an input file with grep.



      $ grep --color cat contents.txt
      cat-1.15
      cat-1.15
      cat-1.15
      cat-1.18
      $ grep --color -z cat contents.txt
      cat-1.15

      cat-1.15

      cat-1.15
      cat-1.18





      share|improve this answer













      With GNU grep this can be accomplished with the -z option.




      -z, --null-data




      Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
      option, this option can be used with commands like sort -z to process arbitrary file names.





      Also this is a UUOC. You can specify an input file with grep.



      $ grep --color cat contents.txt
      cat-1.15
      cat-1.15
      cat-1.15
      cat-1.18
      $ grep --color -z cat contents.txt
      cat-1.15

      cat-1.15

      cat-1.15
      cat-1.18






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 2 hours ago









      Jesse_bJesse_b

      12.3k23065




      12.3k23065








      • 1





        Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

        – Isaac
        17 mins ago














      • 1





        Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

        – Isaac
        17 mins ago








      1




      1





      Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

      – Isaac
      17 mins ago





      Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.

      – Isaac
      17 mins ago













      2














      Another possible solution is to provide two patterns to grep: one will be the actual pattern or term to be searched for, and the second one will be an empty string.



      $ grep --color -e 'cat' -e '' testfile.txt
      cat-1.15

      cat-1.15

      cat-1.15
      cat-1.18


      The -e option is used for specify multiple patterns. From the manual:




      -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a
      pattern beginning with a hyphen (-). (-e is specified by POSIX.)




      You can also combine those patterns as a single extended regular expression, if required:



      $ grep --color -E 'cat|' testfile.txt
      cat-1.15

      cat-1.15

      cat-1.15
      cat-1.18


      Also, you can simply add another pattern to the list if you need to highlight more than one keyword.






      share|improve this answer




























        2














        Another possible solution is to provide two patterns to grep: one will be the actual pattern or term to be searched for, and the second one will be an empty string.



        $ grep --color -e 'cat' -e '' testfile.txt
        cat-1.15

        cat-1.15

        cat-1.15
        cat-1.18


        The -e option is used for specify multiple patterns. From the manual:




        -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a
        pattern beginning with a hyphen (-). (-e is specified by POSIX.)




        You can also combine those patterns as a single extended regular expression, if required:



        $ grep --color -E 'cat|' testfile.txt
        cat-1.15

        cat-1.15

        cat-1.15
        cat-1.18


        Also, you can simply add another pattern to the list if you need to highlight more than one keyword.






        share|improve this answer


























          2












          2








          2







          Another possible solution is to provide two patterns to grep: one will be the actual pattern or term to be searched for, and the second one will be an empty string.



          $ grep --color -e 'cat' -e '' testfile.txt
          cat-1.15

          cat-1.15

          cat-1.15
          cat-1.18


          The -e option is used for specify multiple patterns. From the manual:




          -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a
          pattern beginning with a hyphen (-). (-e is specified by POSIX.)




          You can also combine those patterns as a single extended regular expression, if required:



          $ grep --color -E 'cat|' testfile.txt
          cat-1.15

          cat-1.15

          cat-1.15
          cat-1.18


          Also, you can simply add another pattern to the list if you need to highlight more than one keyword.






          share|improve this answer













          Another possible solution is to provide two patterns to grep: one will be the actual pattern or term to be searched for, and the second one will be an empty string.



          $ grep --color -e 'cat' -e '' testfile.txt
          cat-1.15

          cat-1.15

          cat-1.15
          cat-1.18


          The -e option is used for specify multiple patterns. From the manual:




          -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a
          pattern beginning with a hyphen (-). (-e is specified by POSIX.)




          You can also combine those patterns as a single extended regular expression, if required:



          $ grep --color -E 'cat|' testfile.txt
          cat-1.15

          cat-1.15

          cat-1.15
          cat-1.18


          Also, you can simply add another pattern to the list if you need to highlight more than one keyword.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          HaxielHaxiel

          1,8401510




          1,8401510























              0














              I tried with below command



              sed -r "/cat/{N;s/^$/&=/}" filename


              awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename





              share|improve this answer




























                0














                I tried with below command



                sed -r "/cat/{N;s/^$/&=/}" filename


                awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename





                share|improve this answer


























                  0












                  0








                  0







                  I tried with below command



                  sed -r "/cat/{N;s/^$/&=/}" filename


                  awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename





                  share|improve this answer













                  I tried with below command



                  sed -r "/cat/{N;s/^$/&=/}" filename


                  awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Praveen Kumar BSPraveen Kumar BS

                  1,366138




                  1,366138






























                      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%2f496717%2fhighlight-a-word-without-affecting-the-structure-of-text%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

                      How to label and detect the document text images

                      Vallis Paradisi

                      Tabula Rosettana