1

The following makes sense (though you can't see the contents of debug.cpp).

$ grep '^[A-Za-z_]' debug.cpp
float CheckExistence();
entity Findgoal(float gno);
void RPrint(const string& msg);
void MakeMeDebug(entity who);
void dremove(entity te)
void display_location()
void RPrint(const string& msg)
void MakeMeDebug(entity who)

Now, when I add .*, as follows...

$ grep '^[A-Za-z_].*' debug.cpp

I get 8 blank lines as the result (which I can't seem to make this website display). Strongly suspecting a bug, I updated to the latest grep (version 2.25), and it does the same thing. I am using Ubuntu.

If my understanding is correct, my regular expression says: "Match lines starting with the letters A to Z, case-insensitive, or underscore, followed by anything, including nothing."

Is this a bug? Or am I doing something wrong?

  • have you tried grep -e '...' ? – Eugen Rieck Sep 12 '16 at 15:33
  • Yes. I have also tried -E and -P, but I lost Perl grep functionality when I installed grep 2.25, unfortunately. – Pulseczar Sep 12 '16 at 15:34
  • try putting your terms in parens, or add a length of {1} for your first term. it appears to be acting as an or with your second term. – Frank Thomas Sep 12 '16 at 16:09
  • 1
    What happens if you pipe the results through cat or cat -v? – grawity Sep 12 '16 at 16:14
  • When piped into cat, it shows all 8 lines properly. Any idea why it's not showing the lines when not piped into cat? – Pulseczar Sep 12 '16 at 16:28
3

Your cpp is in DOS format that is its line termination is done by \r\n sequences and you have a 'grep --color=auto' alias existing for grep in your session at the same time. This alias puts terminal controlling escape sequences around the string that matches. If that string is a word character, then it does not bother the output on the terminal. However, if the string contains a carriage return character (\r which matches as the last character of .*), it seems to cause this side effect when it's surrounded by the sequences.

When called using --color=auto, grep recognizes when its output is sent to pipe and then it does not colourize the string, hence it avoids the side effect and appears.

After unaliasing grep, the phenomenon disappears.

To tell you the whole truth, this was not something obvious at all, it took a while for it to strike my mind why the output sent to file is correct even when I use the longer pattern while it shows nothing when the output goes to terminal.

  • 1
    Thank you so much. This problem was racking my brain. This seems to have fixed all kinds of problems that I was having with grep and regular expressions. It wouldn't let me vote you up because my reputation is too low. – Pulseczar Sep 13 '16 at 4:03
  • 1
    No problem at all. It was a fun challenge. – Gombai Sándor Sep 13 '16 at 5:29
  • 1
    Next time you can disable the alias on one line by using \grep instead of grep. So you would have \grep '^[A-Aa-z_]' debug.cpp – DDay Sep 14 '16 at 16:54
  • @DDay Indeed. I've never seen this one-shot unalias trick. – Gombai Sándor Sep 14 '16 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.