0

I know grep can be use to do partial search, for example if i use

echo "Enter example"
read example

echo "Enter Subject"
read subject
grep -i ^$example info.txt | grep -i ^[^,]*:$subject 

The output will be all the lines of string that has, let's say i key in "example", it will display all the lines of string that has the string "example".

I need this function so that the user can do a general search on what are the list of available reading materials there are in the info.txt.

I am currently using if statements, if both $example and $subject have information in it, it will do specific search, like literally string matching string, else it will return false.

Is there anyway to make the search more flexible, or to let it search specifically, like there are many examples in the info.txt.

  1. Example 1:Basic mathematics
  2. Example 2:Advance mathematics
  3. Example 3:How to create a good example ?

So if i key in "example" and "mathematics", it will return false. However, if i key in "example 1" and "basic mathematics", it will echo out the output. I asked around, most of my friends recommended me to use awk, but if i do that , i will have to redo my whole script as I'm using grep in most function to do searches.

My teacher only taught us grep at that point of time, therefore, only understand basic grep and did not know about awk.

  • You could also use sed. But I think it should work anyway, you just have to be careful not to restrict your regular expression too much. grep doesn't do exact matching unless you force it to (for instance, by searching '^example:' instead of just 'example[^:]*:'. And be careful, you should quote "$example" and "$subject" otherwise spaces will result in unexpected behaviour. – orion Jan 13 '15 at 14:59
  • 1
    Try grep -i "^$example.*:.*$subject" info.txt – Costas Jan 13 '15 at 15:03
  • Sorry, typo, re-edited – Zac Jan 13 '15 at 15:12
  • Are you philosophically opposed to shell quotes or something? You keep asking these questions and practically every answer you get says ... "You should quote that..." – mikeserv Jan 13 '15 at 16:19
  • I have no idea what you're talking about @mikeserv – Zac Jan 13 '15 at 16:29
2

Here's a nice little page on shellhacks.com that shows examples "Using BASH "Grep OR / Grep AND / Grep NOT" Operators", and it has the sed & awk equivalents. I'll paste it here, you could use them to search for $subject and/or/not $example, in a certain or any order.

Using BASH "Grep OR / Grep AND / Grep NOT" Operators

In the following article you'll find several examples, that show how to use OR / AND / NOT operators with Linux GREP / SED / AWK commands.

It is quite a common task to use such Linux command line tools as grep, sed and awk for parsing files with regular expressions.

I'll show how to find all lines, that contain any of the several patterns, how to print all lines in a file, that match multiple patterns and how to find and print all lines, that do not match a pattern.

Grep 'OR' Operator

Find all the lines in a file, that match any of the following patterns.

Using GREP command :

grep "pattern1\|pattern2" file.txt
grep -E "pattern1|pattern2" file.txt
grep -e pattern1 -e pattern2 file.txt
egrep "pattern1|pattern2" file.txt

Using AWK command :

awk '/pattern1|pattern2/' file.txt

Using SED command :

sed -e '/pattern1/b' -e '/pattern2/b' -e d file.txt

Grep 'AND' Operator

Find and print all the lines in a file, that match multiple patterns.

Using GREP command :

grep -E 'pattern1.*pattern2' file.txt # in that order
grep -E 'pattern1.*pattern2|pattern2.*pattern1' file.txt # in any order
grep 'pattern1' file.txt | grep 'pattern2' # in any order

Using AWK command :

awk '/pattern1.*pattern2/' file.txt # in that order
awk '/pattern1/ && /pattern2/' file.txt # in any order

Using SED command :

sed '/pattern1.*pattern2/!d' file.txt # in that order
sed '/pattern1/!d; /pattern2/!d' file.txt # in any order

Grep 'NOT' Operator

Find and print all the lines, that do not match a pattern.

Using GREP command :

grep -v 'pattern1' file.txt

Using AWK command :

awk '!/pattern1/' file.txt

Using SED command :

sed -n '/pattern1/!p' file.txt

Here's a link to GNU's Grep manual. It has lots of good info, including on Regular Expressions and some Usage examples.

This page is a "cheat sheet" on RegEx.

  • just another question, i need 1 of the function to find and print the exact pattern like the pattern 1 and pattern 2, have the be exactly what it is in the file.txt before it will display the output – Zac Jan 13 '15 at 15:29
  • It sounds like your "pattern1" will have to be entered exactly then. If there are any common characters before/after a "subject" or "example" entry, you could use those in your pattern... like if a subject always begins with a ": " you could put ": $subject" to avoid matching the wrong part of a different line. I'll add a link to a GNU grep manual at the end – Xen2050 Jan 13 '15 at 15:38
  • there is a criteria whereby if the user input partial search in pattern1 and pattern2, it will give a false. How can i do that in grep, i googled and ask around, most examples are either awk or using array. – Zac Jan 13 '15 at 15:43
  • grep is spec'd for ERE which is better equipped to handle OR/NOT cases - and will do so even within subexpressions. – mikeserv Jan 13 '15 at 16:22
  • that's why i asked, is it possible to get the output i need via the given conditions and searched information, if not, i will probably have to redo my script @mikeserv – Zac Jan 13 '15 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.