0

I basically understand that globbing is used for filename expansion in the shell. I also understand that regular expressions are used for text pattern matching within a file. However, is globbing also used for text pattern matching WITHIN a file?

Any pointers would be appreciated.

Raffa
  • 32,237
Grateful
  • 139
  • 1
  • 5

1 Answers1

1

Can globbing be used to search file contents?

Assuming text file ...

Short answer ... Yes,

Longer answer ... Shell-wise ... In bash as well as some other shells like ksh and zsh you can do word matching like this:

$ for word in one two three; do
    [[ "$word" = tw* ]] && echo "$word"
    done
two

Where the asterisk *(Can also be ? or [...]) will sort of "glob" for alphabet letters in a word(Not for filenames in the current directory) ... That is only if it is used inside the extended test brackets(i.e. the double [[...]] and not the standard single [...]) with the = or == operators ... To understand the globbing behavior in the above example, please compare it with the regular expression matching behavior when the =~ operator(Which enables extended regular expressions on the right side) is used in the below example:

$ for word in one two three; do
    [[ "$word" =~ tw* ]] && echo "$word"
    done
two
three

This text globbing capability of the shell is, however, usable and effective on single words and in most cases you'll need to implement a mechanism to break down whole lines into individual words before you can effectively rely on it for text matching purposes (e.g. you can allow the shell's natural word splitting to happen by not quoting the $line parameter in the next example ... Please notice that this might not be the best mechanism there is, but it serves the purpose of our example here) ... Then you can search for and match text in a file e.g. like so:

$ cat file.txt
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.

$ while IFS= read -r line; do for word in $line; do [[ "$word" = f* ]] && echo "$line" done done < file.txt This is the first line. This is the fourth line. This is the fifth line.

Another example of text(Not filenames) globbing in the shell can be demonstrated in the case ... esac statement like so:

#!/bin/bash

read -p "Enter Y/Yes, y/yes, N/No, n/no or D/d and one character/digit e.g. d2 :" reply

case "$reply" in [Yy]* ) echo "Confirmed ... You entered $reply";; [Nn]* ) echo "Denied ... You entered $reply";; [Dd]? ) echo "Identified ... You entered $reply";;

  • ) echo "Invalid ... You entered $reply";;

esac

Which when run will behave like so:

$ Enter Y/Yes, y/yes, N/No, n/no or D/d and one character/digit e.g. d2 :yeah
Confirmed ... You entered yeah

$ Enter Y/Yes, y/yes, N/No, n/no or D/d and one character/digit e.g. d2 :nope Denied ... You entered nope

$ Enter Y/Yes, y/yes, N/No, n/no or D/d and one character/digit e.g. d2 :D7 Identified ... You entered D7

$ Enter Y/Yes, y/yes, N/No, n/no or D/d and one character/digit e.g. d2 :D77 Invalid ... You entered D77

For other text processing/matching tools like grep, awk, sed ... etc,, an asterisk *(Can also be ? or [...]) usage/behavior is defined by the tool's manual which in most cases will be a regular expression operator/quantifier that will match 0 or more of the preceding regular expression.

For other file search tools like find, ls, locate ... etc., an asterisk *(Can also be ? or [...]) usage/behavior is also defined by the tool's manual which in most cases will be similar to the standard behavior of the shell's globbing character and will expand to the names of files/directories in the specified search directory or the present working directory if no search directory is specified.

Raffa
  • 32,237