Score:0

Can globbing be used to search file contents?

id flag

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 avatar
jp flag
General question ... General answer "yes" ... If you be more specific, we will be more specific ... You are mixing things that don't mix :-)
hr flag
You may find this U&L thread helpful: [grep with Bash-Globbing-like search pattern](https://unix.stackexchange.com/questions/697734/grep-with-bash-globbing-like-search-pattern)
Score:1
jp flag

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.

Grateful avatar
id flag
Much appreciated!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.