|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
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! |
|
#4
|
||||
|
||||
|
we try to write unix shell
Quote:
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:
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > How to escape regular expressions with grep ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|