LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2010, 11:40 AM   #1
twaddlac
Member
 
Registered: Jun 2010
Posts: 34

Rep: Reputation: 0
Question 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?
 
Old 08-10-2010, 11:43 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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.
 
Old 08-10-2010, 02:24 PM   #3
Les Windoze
LQ Newbie
 
Registered: Aug 2010
Location: Florida
Distribution: RHEL5, RHEL6, Solaris 10
Posts: 18

Rep: Reputation: 1
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.
 
Old 08-10-2010, 04:06 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I also think Perl will be better for this. And still, you didn't really explain your problem well enough.
 
Old 08-10-2010, 05:37 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 08-10-2010, 06:33 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by twaddlac View Post
... 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
 
Old 08-10-2010, 08:13 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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
 
1 members found this post helpful.
Old 08-10-2010, 09:10 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by grail View Post
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.
 
1 members found this post helpful.
Old 08-10-2010, 11:44 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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
 
  


Reply

Tags
grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 06:07 PM
Grep file using last specific file patterns jeesun Programming 19 11-24-2009 09:51 PM
grep patterns tekmann33 Linux - Newbie 2 07-14-2008 01:25 PM
how to grep 5k patterns at a time? xiawinter Linux - Software 9 12-29-2007 01:18 AM
Exclude certain patterns from grep? kinetik Linux - General 4 04-24-2006 05:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration