UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 2nd, 2005, 11:28 PM
murugesan murugesan is offline
Dinesh_P_V
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: India
Posts: 256 murugesan New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 11 h 38 m
Reputation Power: 0
Send a message via Yahoo to murugesan
How to escape regular expressions with grep ?

Hi all,

I want to find the number of occurances of each word in a given file.

The following script works fine for normal words.
Code:
#!/bin/sh
cat dinesh_p_v_FileName | tr ' ' '\n' > words
cat words | sort | uniq > uniq_words
for i in `cat uniq_words`
do
        printf "Word : %-20s Count : %-5d\n" $i `grep "$i" words | wc -l`
done
printf "Blank lines count : %d\n" `grep -c '^$' dinesh_p_v_FileName`
rm words uniq_words


However
when there is a word like ^$ in the file grep takes that word as a regular expression. How can I avoid this ?

I cannot escape ^$ to \^\$ since there may be so many regular expressions (of different forms ) in the file.

-Thanks
Murugesan
__________________
Dinesh_P_V

Reply With Quote
  #2  
Old February 3rd, 2005, 04:16 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
grep

I had a similar problem with the $ characters in words. I was not able to escape them and gave up and have written my own version of limited grep in C.

Perhaps somebody knows a better way.

Regards

Reply With Quote
  #3  
Old February 15th, 2005, 02:20 PM
n4mrc n4mrc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 n4mrc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 42 sec
Reputation Power: 0
Lightbulb

I had a similar problem. I was trying to grep -v for a filename that began with a $. (still don't know how that one got created!) I found this and it seems to work for me (ksh on Solaris 9):

grep -v '\$'

Hope that helps!

Reply With Quote
  #4  
Old February 16th, 2005, 10:57 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
we try to write unix shell
Quote:
cat dinesh_p_v_FileName | tr ' ' '\n' > words
cat words | sort | uniq > uniq_words

use
for i in `tr ' ' '\n' dinesh_p_v_FileName¦sort -u`
do
printf "Word : %-20s Count : %-5d\n" $i `grep "$i" words | wc -l`
Quote:
this is stupid, you made an 'unique' sort so $i will always be 1
you already discover the '-c' opt of grep, use it, why that 'wc-l' ?

done
#be a little clear
empty=`grep -c '^$' dinesh_p_v_FileName`
printf "Blank lines count : %d\n" $empty
# i still prefer the old 'echo'
rm -f nothing

NOTA: that 'i' is also a very common programm stupidity
suppose in a 20,000 lines src code you have to change that 'i'
by 'iii'
try it using 'n' or 'c'
__________________
working on Solaris[5-9], preferred languages french and C.

Last edited by guggach : February 16th, 2005 at 11:01 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > How to escape regular expressions with grep ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway