Need help matching a mattern using grep/egrep in bash scripting

grep multiple patterns
grep regex cheat sheet
grep two words at the same time
egrep examples
what is used to filter the lines that start with period followed by a lower alphabet
grep regex tester
grep character classes
grep pattern from file

I am trying to match all characters of given string but those characters should match in the order as given to the bash script.

while [[ $# -gt 0 ]]; do
  case $1 in
       -i)
    arg=$2
    egrep "*[$arg]*" words.txt 
    shift ;;
  esac
  shift
done

$ sh match_the_pattern.sh -i aei words.txt

Should return words like

abstentious
adventitious
sacrilegiousness

If you notice, first a is matched then e and then i, all of them are in order. Plus, the whole word is matched and filtered.

Change this:

arg=$2
egrep "*[$arg]*" words.txt

to this:

arg=$(sed 's/./.*[&]/g' <<< "$2")
grep "$arg" words.txt

If that's not all you need then edit your question to clarify your requirements and provide more truly representative sample input/output.

Using grep (and egrep) with Regular Expressions – Linux Hint, This tutorial describes how to use both grep (and egrep) to find text in files, in their The utilities allow the user to search text files for lines that match a regular characters to allow pattern matching within utilities such as grep, vim and sed. He still delivers courses, in fundamentals, shell scripting and administration, using� Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order. Use one of the following commands to find and print all the lines of a file, that match multiple patterns. Using grep command (exact order): $ grep -E 'PATTERN1.*PATTERN2' FILE. Using grep command (any order):

You may use getopts with some bash parameter substitution to construct the query string.

#!/bin/bash
while getopts 'i:' choice
do
  case "${choice}" in
    i)
     length=${#OPTARG}
     for((count=0;count<length;count++))
     do
      if [ $count -eq 0 ]
      then 
       pattern="${pattern}.*${OPTARG:count:1}.*"
      else
       pattern="${pattern}${OPTARG:count:1}.*"
      fi
     done
    ;;    
  esac
done
# The remaining parameter should be our filename
shift $(($OPTIND - 1))
filename="$1"
# Some error checking based on the parsed values
# Ideally user input should not be trusted, so a security check should
# also be done,omitting that for brevity.
if [ -z "$pattern" ] || [ -z "$filename" ]
then
 echo "-i is must. Also, filename cannot be empty"
 echo "Run the script like ./scriptname -i 'value' -- filename"
else
 grep -i "${pattern}" "$filename"
fi

Refer this to know more on parameter substitution and this for getopts.

Regular expressions in grep ( regex ) with examples, Regular Expressions is nothing but a pattern to match for each input line. vivek: x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash I hope following table will help you quickly understand regular expressions in grep when using under egrep implementations support \{ instead, so portable scripts should� I have a requirement in which I would need to grep/find a line based on matching 3 different patterns. I know we can grep with -E multiple parameters seprated by pipe but this work Pipe (|) as OR condition. My requirement is I want to use AND condition. It should show line where it satisfy both the parameter. e.g. line in a file as —

Your regex is matching 'a' or 'e' or 'i' because they are in a character set ([]). I think the regular expression you are looking for is

a+.*e+.*i+.*

which matches 'a' one or more times, then anything, then 'e' one or more times, then anything, then 'i' one or more times.

grep, egrep, fgrep -- match patterns in a file, fgrep searches files for one or more pattern arguments. egrep works in a similar way, but uses extended regular expression matching (as well as the If you are writing scripts to run with both Linux and MKS Toolkit, you should use -- binary� grep is a basic program used for pattern matching and it was written in the 70s along with the rest of the UNIX tool that we know and love (or hate). Learning grep has a lot more to it than regexes. To get started with it and to see the beauty and elegance of grep you need to see some real-world examples, first.

Grep OR - Grep AND - Grep NOT, GREP NOT: print the lines, that do not match a pattern (negative matching). and awk are the most common Linux command line tools for parsing files. patterns with the OR , AND , NOT operators, using grep , egrep , sed Bash one liner for the true Linux admins! Try 'grep –help' for more information. Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I (8 Replies)

grep(1): print lines matching pattern, grep, egrep, fgrep - print lines matching a pattern --help. Print a usage message briefly summarizing these command-line options and the -c, --count: Suppress normal output; instead print a count of matching lines for each input file. Portable shell scripts should avoid both -q and -s and should redirect standard and� I'm new to egrep. What pattern could I use to find all lines that match this pattern: <beginning of line><any amount of whitespace>sub<space>. I want it to return the entire line. (I'm trying to generate a list of all Perl sub definitions in a list of Perl modules.) Thanks for your help! (7 Replies)

egrep command in Linux with examples, Options: Most of the options for this command are same as grep. -c: Used to counts and prints the number of lines that matched the pattern and� When you want do find out how many lines that does not match the pattern $ grep -v -c this demo_file 4 12. Display only the file names which matches the given pattern using grep -l. If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-case L) option.

Comments
  • You could probably do grep "$arg" "$3" if the command is sh script.sh -i aei words.txt.
  • Yes, there are several other "opportunities" for improvement in the OPs script :-).
  • Your solution is quite good, although putting a static "words.txt" in the grep seems redundant since it appears he already includes it as an argument in his command, that's really all I was pointing out. Cheers!
  • I completely understand, but I'm just showing the OP how to do what she specifically asked about and keeping the changes from her script minimal so what she needs to do for that specific problem is as clear as possible rather thatn muddying it with other unrelated changes. The point in my comment is just that there are several other things that could also be improved on in her script, including the hard-coded file name.
  • Yep, understandable; I'm really not one of those people that expects someone answering a question to rewrite the persons entire script either, it's a nice simple solution either way (upvoted earlier).
  • Clearly the op need to construct such a pattern given the argument to the -i parameter which forms the core of the problem.