Skip to main content
added 168 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The command you're looking for is grep, and the regular expression you want is b[[:alnum:]]*[ig][[:alnum:]]*o.

  • [[:alnum:]] will match a single alphanumeric character.

    [[:alnum:]] will match a single alphanumeric character.
  • * will match any number (including zero) of the previous expression.

    * will match any number (including zero) of the previous expression.
  • [ig] will match a single i or g.

    [ig] will match a single i or g.
  • All other characters (b and o) in this particular regular expression matches themselves.

    All other characters (b and o) in this particular regular expression matches themselves.

The use of [[:alnum::]]* rather than .* avoids matching words that contain spaces.

grep is used like

grep OPTIONS 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

In this case, you would want to use the -w and -o options, which forces the expression to match words (strings of characters surrounded by non-word characters) and to only return the matched data (not the whole line).

$ grep -w -o 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio
bgo

You mentioned that you wanted to highlight the matched words. This is something that GNU grep can do:. I'm dropping the -o option here to get the whole line of each match, otherwise you'll just get the same result as previously, but highlighted, which would be boring.

$ grep --color -w 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
$ grep --color -w 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio jdjjf
dgdhd bgo

As you can see, this only shows the matches on lines that contain matches. To see the full input line(even lines with no match), with the matches highlighted, we have to drop the -w option and do

$ grep --color '\bb[[:alnum:]]*[ig][[:alnum:]]*o\b|$' words

$ grep --color -E '\bb[[:alnum:]]*[ig][[:alnum:]]*o\b|$' words
boo djhg
bio jdjjf
dgdhd bgo
ghhh

We had to add the -E option since | is an extended regular expression. The \b will match at any word boundary.

The command you're looking for is grep, and the regular expression you want is b[[:alnum:]]*[ig][[:alnum:]]*o.

  • [[:alnum:]] will match a single alphanumeric character.

  • * will match any number (including zero) of the previous expression.

  • [ig] will match a single i or g.

  • All other characters (b and o) in this particular regular expression matches themselves.

grep is used like

grep OPTIONS 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

In this case, you would want to use the -w and -o options, which forces the expression to match words (strings of characters surrounded by non-word characters) and to only return the matched data (not the whole line).

$ grep -w -o 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio
bgo

You mentioned that you wanted to highlight the matched words. This is something that GNU grep can do:

$ grep --color -w 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio jdjjf
dgdhd bgo

As you can see, this only shows the matches on lines that contain matches. To see the full input line with the matches highlighted, we have to drop the -w option and do

$ grep --color '\bb[[:alnum:]]*[ig][[:alnum:]]*o\b|$' words
boo djhg
bio jdjjf
dgdhd bgo
ghhh

We had to add the -E option since | is an extended regular expression. The \b will match at any word boundary.

The command you're looking for is grep, and the regular expression you want is b[[:alnum:]]*[ig][[:alnum:]]*o.

  • [[:alnum:]] will match a single alphanumeric character.
  • * will match any number (including zero) of the previous expression.
  • [ig] will match a single i or g.
  • All other characters (b and o) in this particular regular expression matches themselves.

The use of [[:alnum::]]* rather than .* avoids matching words that contain spaces.

grep is used like

grep OPTIONS 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

In this case, you would want to use the -w and -o options, which forces the expression to match words (strings of characters surrounded by non-word characters) and to only return the matched data (not the whole line).

$ grep -w -o 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio
bgo

You mentioned that you wanted to highlight the matched words. This is something that GNU grep can do. I'm dropping the -o option here to get the whole line of each match, otherwise you'll just get the same result as previously, but highlighted, which would be boring.

$ grep --color -w 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio jdjjf
dgdhd bgo

As you can see, this only shows the matches on lines that contain matches. To see the full input (even lines with no match), with the matches highlighted, we have to drop the -w option and do


$ grep --color -E '\bb[[:alnum:]]*[ig][[:alnum:]]*o\b|$' words
boo djhg
bio jdjjf
dgdhd bgo
ghhh

We had to add the -E option since | is an extended regular expression. The \b will match at any word boundary.

added 863 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The command you're looking for is grep, and the regular expression you want is ^b.*[ig].*o$b[[:alnum:]]*[ig][[:alnum:]]*o.

grep is used like

grep 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

  • The ^ anchors the expression to the start of the line, and the $[[:alnum:]] does the same but to the endwill match a single alphanumeric character.

  • . matches any single character and * matches any number of the previous expression, so .* matcheswill match any number (including zero) of any characterthe previous expression.

  • [ig] will match a single i or g.

  • All other characters (b and o) in this particular regular expression matches themselves.

grep is used like

grep OPTIONS 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

In this case, you would want to use the -w and -o options, which forces the expression to match words (strings of characters surrounded by non-word characters) and to only return the matched data (not the whole line).

$ grep -w -o 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio
bgo

You mentioned that you wanted to highlight the matched words. This is something that GNU grep can do:

$ grep --color -w 'b[[:alnum:]]*[ig][[:alnum:]]*o' words

bio jdjjf
dgdhd bgo

As you can see, this only shows the matches on lines that contain matches. To see the full input line with the matches highlighted, we have to drop the -w option and do

$ grep --color '\bb[[:alnum:]]*[ig][[:alnum:]]*o\b|$' words

boo djhg
bio jdjjf
dgdhd bgo
ghhh

We had to add the -E option since | is an extended regular expression. The \b will match at any word boundary.

The command you're looking for is grep, and the regular expression you want is ^b.*[ig].*o$.

grep is used like

grep 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

  • The ^ anchors the expression to the start of the line, and the $ does the same but to the end.

  • . matches any single character and * matches any number of the previous expression, so .* matches any number (including zero) of any character.

  • [ig] will match a single i or g.

  • All other characters (b and o) in this particular regular expression matches themselves.

The command you're looking for is grep, and the regular expression you want is b[[:alnum:]]*[ig][[:alnum:]]*o.

  • [[:alnum:]] will match a single alphanumeric character.

  • * will match any number (including zero) of the previous expression.

  • [ig] will match a single i or g.

  • All other characters (b and o) in this particular regular expression matches themselves.

grep is used like

grep OPTIONS 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

In this case, you would want to use the -w and -o options, which forces the expression to match words (strings of characters surrounded by non-word characters) and to only return the matched data (not the whole line).

$ grep -w -o 'b[[:alnum:]]*[ig][[:alnum:]]*o' words
bio
bgo

You mentioned that you wanted to highlight the matched words. This is something that GNU grep can do:

$ grep --color -w 'b[[:alnum:]]*[ig][[:alnum:]]*o' words

bio jdjjf
dgdhd bgo

As you can see, this only shows the matches on lines that contain matches. To see the full input line with the matches highlighted, we have to drop the -w option and do

$ grep --color '\bb[[:alnum:]]*[ig][[:alnum:]]*o\b|$' words

boo djhg
bio jdjjf
dgdhd bgo
ghhh

We had to add the -E option since | is an extended regular expression. The \b will match at any word boundary.

added 2 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The command you're looking for is grep, and the regular expression you want is ^b.*[ig].*o$.

grep is used like

grep EXPRESSION'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

  • The ^ anchors the expression to the start of the line, and the $ does the same but to the end.

  • . matches any single character and * matches any number of the previous expression, so .* matches any number (including zero) of any character.

  • [ig] will match a single i or g.

  • All other characters (b and o) in this particular regular expression matches themselves.

The command you're looking for is grep, and the regular expression you want is ^b.*[ig].*o$.

grep is used like

grep EXPRESSION INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

  • The ^ anchors the expression to the start of the line, and the $ does the same but to the end.

  • . matches any character and * matches any number of the previous expression, so .* matches any number (including zero) of any character.

  • [ig] will match a single i or g.

  • All other characters in this regular expression matches themselves.

The command you're looking for is grep, and the regular expression you want is ^b.*[ig].*o$.

grep is used like

grep 'EXPRESSION' INPUT-FILES

and will output the lines matching EXPRESSION to its standard output (the terminal, in this case).

  • The ^ anchors the expression to the start of the line, and the $ does the same but to the end.

  • . matches any single character and * matches any number of the previous expression, so .* matches any number (including zero) of any character.

  • [ig] will match a single i or g.

  • All other characters (b and o) in this particular regular expression matches themselves.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading