1

I have a text file with the name "hello.txt" that contains the following two lines:

Hello WORLD!
Hallo world!

When I wrote the following

grep -i h[ae]llo hello.txt 

in the terminal, the output was nothing. I expected the print out of the two lines.

6
  • 6
    What happens when you enclose the expresion in quotes? grep -i 'h[ae]llo' hello.txt
    – choroba
    Apr 14, 2015 at 20:47
  • What grep are you using(grep --version)? What shell are you using? Are you grepping the correct file? Apr 14, 2015 at 20:59
  • @richard I am using bash
    – Altaïr
    Apr 14, 2015 at 21:02
  • 1
    When you entered that grep command, did you get no output then a shell prompt, or did the shell appear to hang, as if awaiting user input? What does this command output: shopt nullglob? Apr 14, 2015 at 21:09
  • @glennjackman why would nullglob be relevant? The OP might observe this behavior if they have one file named hallo and one named hello in the pwd, but how would the nullglob option affect it?
    – terdon
    Apr 15, 2015 at 8:38

1 Answer 1

2

It's possible some kind of character set problem is throwing things off for you. Here are some commands you should be able to use to re-create a successful usage of grep that may help you.

First let's make a text file in the terminal to avoid any character set problems:

echo -e "Hello\nHallo" > foo.txt

The -e flag tells echo we want the \n to be interpreted as a newline, and not the literal sequence. This gives us two lines. You should be able to cat the file and see it without any issue, like this:

cat foo.txt
Hello
Hallo

Now you can use the grep command you posted before (it doesn't actually need quotes, but you should use them as there are special characters in bash. Also, single quotes not double quotes)

grep -i 'h[ae]llo' foo.txt

This produces the expected result. If this worked for you, but you are curious as to what is going on with your other text file, you can do a few things to figure out what is going on.

First, try running the file command on the file. This should tell you it's a text file, and what encoding it appears to be in.

file foo.txt
foo.txt: ASCII text

If you get something strange or in a different character encoding, that's going to be a problem. You can also inspect the hexidecimal dump of the file by:

hexdump -C foo.txt

00000000  48 65 6c 6c 6f 0a 48 61  6c 6c 6f 0a              |Hello.Hallo.|
0000000c

Also as a bonus here is a in-browser Linux emulator you can use that might help you toy around:

http://bellard.org/jslinux/

It even has GCC so you can compile C code!

You must log in to answer this question.

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