Operating System - HP-UX
1829837 Members
2251 Online
109993 Solutions
New Discussion

pattern search (shell script)

 
Sanjay Jadhav
Occasional Contributor

pattern search (shell script)

Hello friends,
Can anybody tell me how to do a pattern search for more than one character... example: I want search words whose last character is "c" or "h" or "at".
Thanx in advance!

-Sanjay
Aaryan - Simply the best!
12 REPLIES 12
H.Merijn Brand (procura
Honored Contributor

Re: pattern search (shell script)

case $x in
*c|*h|*at) : blah
;;
*x|*fdg|*gurgl) : booh
;;
*) exit ;;
esac
Enjoy, Have FUN! H.Merijn
Jean-Luc Oudart
Honored Contributor

Re: pattern search (shell script)

Multiple pattern search
man grep
cat file | grep -e "at" -e "h" -e "c"

Jean-Luc
fiat lux
H.Merijn Brand (procura
Honored Contributor

Re: pattern search (shell script)

Jean-Luc: "last character" =

# ls -1 | egrep '(c|h|at)$'

And 'cat' is (again) completely redundant and only cluttering the resources with an extra process:

# grep -e at -e h -e c < file

would perform your action a lot faster if the system is close to it's process limit
Enjoy, Have FUN! H.Merijn
Jean-Luc Oudart
Honored Contributor

Re: pattern search (shell script)

Agreed,
This is if the filter is to be applied on the output of a program/application.
I simulated this pgm with cat file

otherwise
grep -e "at" -e "h" -e "c" filename

Jean-Luc
fiat lux
harry d brown jr
Honored Contributor

Re: pattern search (shell script)

How/Where are you going to use it?

here's an example:

cat somefile | grep -e "c$" -e "h$" -e "at$"


live free or die
harry
Live Free or Die
Sanjay Jadhav
Occasional Contributor

Re: pattern search (shell script)

Thanks friends!!!
Harry, actually I wanted to findout all the words from the text file which ends with "c", "h" or "at" (two chars).

Thanks.

-Sanjay
Aaryan - Simply the best!
harry d brown jr
Honored Contributor

Re: pattern search (shell script)

AHHHHHHH

WORDS is the keyword here!

try this


cat YOURFILE | grep -e "[c|h][ |$]" -e "at[ |$]"


live free or die
harry

Live Free or Die
Sanjay Jadhav
Occasional Contributor

Re: pattern search (shell script)

Okay, Harry it's for YOU !!!
Can I list the files (using ls) and combing your regular expression but with not using grep? ls command should list only the files which ends with c or h or at. I do not want to use command like "ls *c *h *at". Here you can also use find command, which will be better.
After the output of this command, I want to pass it to grep which will find the specific words in these files.

-Sanjay
Aaryan - Simply the best!
H.Merijn Brand (procura
Honored Contributor

Re: pattern search (shell script)

"Words" or "Sentences with words"?

Words:

perl -nle '$w{$1}++while/\b(\w+(?:a|t|ch))\b/g;END{print"$_ ($w{$_})"for sort keys%w}' file

Sentences:

perl -ne '/(a|t|ch)\b/ and print' file
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: pattern search (shell script)


Yes,

cd 2directory2search

find . -type f -name "*[c|h]$" -o -name "*at$" -exec grep -l WORDPATTERN_HERE {} \;

example:

cd 2directory2search

find . -type f -name "*[c|h]$" -o -name "*at$" -exec grep -l "[Ss]anjay[ |$]" {} \;


live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: pattern search (shell script)


# cd 2directory2search
# perl -MFile::Find -nle
'find(sub{-f&&-s&&/(a|t|ch)$/ or return;local$/;open F,"<$_"or return;=~/[Ss]anjay/ and print $File::Find::name},".")'
Enjoy, Have FUN! H.Merijn
Jordan Bean
Honored Contributor

Re: pattern search (shell script)

It appears that you want to search for words in a text file from all the C source and header files plus some at files (?) within a directory. Assuming c, h, and at are filename extensions and the text file, named tfile, contains fixed strings, try this:

find . -type f -name '*.{c,h,at}' | xargs grep -Ff tfile