ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am interested in using the grep method in the shell of my CentOS machine to obtain patterns from a file and use them to search through another file and highlight the patterns found. For example:
pattern file:
one
two
three
test file:
AAAAAAAAAAAAAAAAAAAAAoneAAAAAAAAAAAAAAAAthreeAAAAAAAAAAAA
If you're doing a lot of this, you might want to use Perl. I've done something like that in the past where the perl script uses a text file as a dictionary of things to grep for.
Perl is certainly capable of meeting the stated requirements, and indeed is probably a good tool if the problem expands very much. Still, grep in a bash script is more than up to the task. Without actually doing the OP's (home)work, he wants to load a variable from the pattern file in a while loop, and for each iteration of the loop, use that variable as the pattern argument to grep. Should take about 3 or four lines.
--- rod.
... another file and highlight the patterns found.
If you want to highlight the found strings, you should change them from normal string to a highlight escape code sequence version using sed and not grep. You should also put all the patterns in one statement so that the found strings will not get separated in every search.
Code:
shopt -s extglob
# void f (string <patterns file path>, string <test file path>)
#
function f {
local REPLY
local -a PATTERNS=()
while read; do
[[ -z $REPLY || $REPLY == *([[:blank:]])'#'* ]] && continue
PATTERNS[${#PATTERNS[@]}]='-e'
PATTERNS[${#PATTERNS[@]}]=s/$REPLY/$'\e'\\[0;1m\&$'\e'\\[0m/g
done < "$1"
if [[ ${#PATTERNS[@]} -eq 0 ]]; then
echo "error: no pattern was found in $1." # >&2
return 1
fi
echo sed "${PATTERNS[@]}" "$2"
sed "${PATTERNS[@]}" "$2"
# (return)
}
f patterns.txt test.txt
Last edited by konsolebox; 08-10-2010 at 06:38 PM.
Reason: no pattern check
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.