function::matched_str - Linux


The function::matched_str command is a versatile tool for extracting the first occurrence of a given string pattern from input. It proves particularly useful in text processing and data extraction tasks.


function::matched_str '<pattern>' [options] '<string>'

Required Arguments:

  • <pattern>: A regular expression pattern to search for.

  • <string>: The input string to search within.


  • -g: Enable global search, returning all matching occurrences.
  • -i: Ignore case during the search.
  • -x: Perform an exact match, requiring the entire string to match the pattern.

Default Value:

  • If no options are specified, the operation performs a single case-sensitive search.


Extract the first email address from a text:

function::matched_str '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}' "John Doe <>"

Find all phone numbers in a contact list:

function::matched_str -g '0[0-9]{9}' "Bob Smith: 0123456789\nJane Doe: 0987654321"

Common Issues

  • Ensure the regular expression pattern is valid and tailored to the specific task.
  • Check for potential edge cases and empty input strings.
  • Handle potential side effects when using the -g option, as multiple matches can be returned.


function::matched_str can be combined with other Linux commands for advanced data manipulation tasks, such as:

  • Piping output to other commands: function::matched_str 'pattern' | command
  • Using it within scripts to automate complex string search operations.
  • Integrating it with other text-processing tools like awk and sed.

Related Commands

  • grep: Search for lines that match a given pattern.
  • sed: Perform text substitutions and modifications.
  • awk: Extract and process data from text files.