Don't use *
for this. Use grep 'ki' trial_file.txt
or grep -F 'ki' trial_file.txt
.
Unless you pass it the -x
/--line-regex
option, grep
will return lines that contain a match anywhere, even if the whole line isn't a match. So all you have to do is match part of the line. You don't have to do anything special to indicate there may be more characters.
In a regular expression, *
means "zero or more of the previous item." This is an entirely different from its meaning in shell pathname expansion (see also this article, man 7 glob
, and this section). So, for example:
ax*b
matches a
, followed by any number of x
es (even none), followed by b
: ab
, axb
, axxb
, axxxb
, ...
a[xz]*b
matches a
followed by any number of characters where each is x
or z
, followed by b
: ab
, axb
, azb
, axxb
, axzb
, azxb
, azzb
, axxxb
, ...
a(xyz)*b
matches a
, followed zero or more occurrences of the string xyz
, followed by b
: ab
, axyzb
, axyzxyzb
, axyzxyzxyzb
, ...
In this case, it seems like you're just searching for text. You don't need to use any regular expression metacharacters like .
, *
, or \
that have special meanings. That's why I suggest passing the -F
flag, which makes grep
search for "fixed strings" rather than performing regular expression matching.
If, however, you only want to match starting at the beginning of the line, then you do want to use a regular expression metacharacter: ^
, as mjb2kmn suggests. This anchors your match to the start of the line. In that case you would run grep '^ki' trial_file.txt
.
For more information on the options grep
supports, see man grep
and the GNU Grep manual.
Although in general I suggest enclosing regular expressions in '
'
quotes, in this case no quoting is necessary because the shell does not perform any expansions on ki
or ^ki
before passing them to grep
.
grep ki trial_file.txt
. The*
is wrong. – Pilot6 Sep 18 '17 at 18:32grep
matches the whole line and requires something special to be done to just match part of a line. Both mjb2kmn's answer and my answer address that, but it would be outside the scope of that question. (That question also has a major part that doesn't apply here--the issue of when the shell expands*
--though currently no answers there really address that in detail.) – Eliah Kagan Sep 18 '17 at 18:49