3

Using this simple pastebin test string, why would the last example using a regex pattern seen here give me two blank lines in comparison to the other two? I would expect to see the same result as example1. Note, the only change from example2 to example3 is the switch from option -l to option -n.

**example1**
$ grep -rnw html5uploader
    views/layouts/test.ctp:49:       src="/js/jquery.html5uploader.min.js<?=$no_cache?>">
    views/layouts/test.ctp:52:       $("#screenshot").html5uploader(

**example2**
$ grep -rlw .*html5uploader.*
    views/layouts/test.ctp

**example3**
$ grep -rnw .*html5uploader.*
    (empty line)
    (empty line)

The pastebin contents for file "test.ctp":

<!-- 
        <input type="file" name="screenshot" id="screenshot" multiple />


        <script type="text/javascript" src="/js/jquery.html5uploader.min.js<?=$no_cache?>"></script>
        <script type="text/javascript">
            // File upload
            $("#screenshot").html5uploader(
            {
                name: "screenshot",
                postUrl: "/bugs/ajax_upload_files"
            });
        </script>
 -->    </body>
</html>
2
  • 2
    Please include the pastebin code in your question.
    – Jeff Schaller
    Commented Jan 18, 2018 at 23:07
  • As an aside, always put your regex in quotes: grep -rlw '.*html5uploader.*'. Without the quotes it is subject to both word splitting and pathname expansion and these are frequent causes of unwanted surprises.
    – John1024
    Commented Jan 18, 2018 at 23:14

1 Answer 1

5

The problem is DOS/Windows line-endings in your source file.

Observe that the first command below appears to produce blank lines while the second doesn't:

$ grep -r '.*html5uploader.*'


$ grep -r '.*html5uploader.*' | hexdump -C
00000000  74 65 73 74 2e 63 74 70  3a 09 09 3c 73 63 72 69  |test.ctp:..<scri|
00000010  70 74 20 74 79 70 65 3d  22 74 65 78 74 2f 6a 61  |pt type="text/ja|
00000020  76 61 73 63 72 69 70 74  22 20 73 72 63 3d 22 2f  |vascript" src="/|
00000030  6a 73 2f 6a 71 75 65 72  79 2e 68 74 6d 6c 35 75  |js/jquery.html5u|
00000040  70 6c 6f 61 64 65 72 2e  6d 69 6e 2e 6a 73 3c 3f  |ploader.min.js<?|
00000050  3d 24 6e 6f 5f 63 61 63  68 65 3f 3e 22 3e 3c 2f  |=$no_cache?>"></|
00000060  73 63 72 69 70 74 3e 0d  0a 74 65 73 74 2e 63 74  |script>..test.ct|
00000070  70 3a 09 09 09 24 28 22  23 73 63 72 65 65 6e 73  |p:...$("#screens|
00000080  68 6f 74 22 29 2e 68 74  6d 6c 35 75 70 6c 6f 61  |hot").html5uploa|
00000090  64 65 72 28 0d 0a                                 |der(..|
00000096

Observe the byte sequence 0a 0d in the above. That indicates DOS/Windows line-endings.

The following produces the desired text:

$ tr -d '\r' <test.ctp | grep '.*html5uploader.*'
                <script type="text/javascript" src="/js/jquery.html5uploader.min.js<?=$no_cache?>"></script>
                        $("#screenshot").html5uploader(

                        $("#screenshot").html5uploader(

Thus, the issue is that the source file, test.ctp, has DOS/Windows line-endings which, when a complete line is printed, cause the output to be overwritten.

Use tr -d '\r' or one of the convenient dos2unix utilities to correct the files.

For a discussion of which editors can be used to avoid this problem, see "Which text editors handle both Windows- and Unix-style line breaks properly?"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .