LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep Patterns from File (https://www.linuxquestions.org/questions/programming-9/grep-patterns-from-file-825366/)

twaddlac 08-10-2010 11:40 AM

grep Patterns from File
 
Hello,

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

Any suggestions?

GrapefruiTgirl 08-10-2010 11:43 AM

You should get some ideas from this thread:
http://www.linuxquestions.org/questi...script-821689/

Then, show is what you've come up with if you're still having troubles, and we shall see if we can help you fix it.

Les Windoze 08-10-2010 02:24 PM

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.

MTK358 08-10-2010 04:06 PM

I also think Perl will be better for this. And still, you didn't really explain your problem well enough.

theNbomr 08-10-2010 05:37 PM

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.

konsolebox 08-10-2010 06:33 PM

Quote:

Originally Posted by twaddlac (Post 4062042)
... 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


grail 08-10-2010 08:13 PM

Quote:

Originally Posted by konsolebox
If you want to highlight the found strings

Couldn't we just use grep to highlight?
Code:

grep --color -f pattern_file test_file

konsolebox 08-10-2010 09:10 PM

Quote:

Originally Posted by grail (Post 4062478)
Couldn't we just use grep to highlight?
Code:

grep --color -f pattern_file test_file

I didn't know --color. Yes it works and is better.

grail 08-10-2010 11:44 PM

Quote:

I didn't know --color. Yes it works and is better.
That's ok ... I didn't know either till I started using Ubuntu and it was switched on as standard :)


All times are GMT -5. The time now is 10:56 AM.