0

I expected grep * filename to print every line of filename since I thought * would match every line of this file.

$ cat filename
first line
second line
*
$ grep * filename    #Why won't this print anything?
$ 

Why didn't it?

3

First issue:

If you put * in a shell directly without quoting it, it will be expanded by the shell to the list of files in the current directory before the command even gets executed.

Second issue:

grep by default uses regular expressions for matching patterns. In regexp * means "match a pattern zero or more times" while period (.) means "match any character".

So what you want is something like: grep ".*" filename. Or just use cat

2
grep ^ filename

will print every line, because every line has a beginning, and that is what ^ matches. (Depending on the regular expression implementation, it could well be more efficient than matching .*)

grep . filename

deletes completely empty lines (lines without anything including spaces and tabs).

This command is more use if you have multiple files, as grep by default prints the filename. For example on my system

grep ^ /proc/sys/kernel/shm*

produces

/proc/sys/kernel/shmall:18446744073692774399
/proc/sys/kernel/shmmax:18446744073692774399
/proc/sys/kernel/shmmni:4096
/proc/sys/kernel/shm_next_id:-1
/proc/sys/kernel/shm_rmid_forced:0

so I can see the 5 values with a single command.

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.