highlight a word without affecting the structure of text [duplicate]












3
















This question already has an answer here:




  • Convince grep to output all lines, not just those with matches

    11 answers




$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















marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    3
















    This question already has an answer here:




    • Convince grep to output all lines, not just those with matches

      11 answers




    $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















    marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      3












      3








      3









      This question already has an answer here:




      • Convince grep to output all lines, not just those with matches

        11 answers




      $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

















      This question already has an answer here:




      • Convince grep to output all lines, not just those with matches

        11 answers




      $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 ?





      This question already has an answer here:




      • Convince grep to output all lines, not just those with matches

        11 answers








      awk sed grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 at 17:09







      user607694

















      asked Jan 25 at 17:07









      user607694user607694

      504




      504




      marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          4 Answers
          4






          active

          oldest

          votes


















          5














          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



















          • 2





            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
            Jan 25 at 19:51



















          4














          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

































            1














            A couple more possibilities with grep:



            grep for 0 or more instances of cat:



            grep --color '(cat)*' contents.txt


            grep for cat or the empty string:



            grep -E --color 'cat|' contents.txt


            (The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)





            Alternatively you can use sed to do the colourization manually using ANSI escape codes:



            red='c[[1;31m'
            default='c[[0m'
            sed "s/cat/${red}cat${default}/g" contents.txt


            Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.






            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
























              • How do these highlight the output?

                – wjandrea
                Jan 25 at 20:50


















              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              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



















              • 2





                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
                Jan 25 at 19:51
















              5














              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



















              • 2





                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
                Jan 25 at 19:51














              5












              5








              5







              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 Jan 25 at 17:14









              Jesse_bJesse_b

              12.5k23066




              12.5k23066








              • 2





                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
                Jan 25 at 19:51














              • 2





                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
                Jan 25 at 19:51








              2




              2





              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
              Jan 25 at 19:51





              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
              Jan 25 at 19:51













              4














              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






























                4














                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




























                  4












                  4








                  4







                  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








                  edited Jan 26 at 6:03

























                  answered Jan 25 at 17:35









                  HaxielHaxiel

                  2,3001812




                  2,3001812























                      1














                      A couple more possibilities with grep:



                      grep for 0 or more instances of cat:



                      grep --color '(cat)*' contents.txt


                      grep for cat or the empty string:



                      grep -E --color 'cat|' contents.txt


                      (The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)





                      Alternatively you can use sed to do the colourization manually using ANSI escape codes:



                      red='c[[1;31m'
                      default='c[[0m'
                      sed "s/cat/${red}cat${default}/g" contents.txt


                      Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.






                      share|improve this answer






























                        1














                        A couple more possibilities with grep:



                        grep for 0 or more instances of cat:



                        grep --color '(cat)*' contents.txt


                        grep for cat or the empty string:



                        grep -E --color 'cat|' contents.txt


                        (The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)





                        Alternatively you can use sed to do the colourization manually using ANSI escape codes:



                        red='c[[1;31m'
                        default='c[[0m'
                        sed "s/cat/${red}cat${default}/g" contents.txt


                        Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.






                        share|improve this answer




























                          1












                          1








                          1







                          A couple more possibilities with grep:



                          grep for 0 or more instances of cat:



                          grep --color '(cat)*' contents.txt


                          grep for cat or the empty string:



                          grep -E --color 'cat|' contents.txt


                          (The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)





                          Alternatively you can use sed to do the colourization manually using ANSI escape codes:



                          red='c[[1;31m'
                          default='c[[0m'
                          sed "s/cat/${red}cat${default}/g" contents.txt


                          Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.






                          share|improve this answer















                          A couple more possibilities with grep:



                          grep for 0 or more instances of cat:



                          grep --color '(cat)*' contents.txt


                          grep for cat or the empty string:



                          grep -E --color 'cat|' contents.txt


                          (The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)





                          Alternatively you can use sed to do the colourization manually using ANSI escape codes:



                          red='c[[1;31m'
                          default='c[[0m'
                          sed "s/cat/${red}cat${default}/g" contents.txt


                          Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 25 at 22:02

























                          answered Jan 25 at 21:49









                          Digital TraumaDigital Trauma

                          5,88211528




                          5,88211528























                              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
























                              • How do these highlight the output?

                                – wjandrea
                                Jan 25 at 20:50
















                              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
























                              • How do these highlight the output?

                                – wjandrea
                                Jan 25 at 20:50














                              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 Jan 25 at 19:07









                              Praveen Kumar BSPraveen Kumar BS

                              1,434138




                              1,434138













                              • How do these highlight the output?

                                – wjandrea
                                Jan 25 at 20:50



















                              • How do these highlight the output?

                                – wjandrea
                                Jan 25 at 20:50

















                              How do these highlight the output?

                              – wjandrea
                              Jan 25 at 20:50





                              How do these highlight the output?

                              – wjandrea
                              Jan 25 at 20:50



                              Popular posts from this blog

                              Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                              ComboBox Display Member on multiple fields

                              Is it possible to collect Nectar points via Trainline?