SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
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 April 9th, 2008, 10:22 AM
rudek01 rudek01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 3 rudek01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 27 sec
Reputation Power: 0
Question I have stumped 2 unix scripters already

I have hundreds even thousands of files where each file has a ^ field separator and a ~ line separator. there are no carriage returns or line feeds at all in these files. So the file is basically just one big long line of text. I need to count the number of times the text "PO1" appears in each of these files in this directory. I am running the script or I can run from the command line, in the directory where these files reside, so, I have tried the awk, grep, sed commands, nothing works, I have received assistance from 2 very experienced unix script developers and they are stumped also. any ideas on how to get this done?

Reply With Quote
  #2  
Old April 9th, 2008, 11:40 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 591 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 51 m 38 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
My guess is that you have not stumped anyone here - and if you have they are not very good at what they do...
That aside, what have you tried so far that has not worked? What has it produced?
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #3  
Old April 9th, 2008, 12:10 PM
rudek01 rudek01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 3 rudek01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by L7Sqr
My guess is that you have not stumped anyone here - and if you have they are not very good at what they do...
That aside, what have you tried so far that has not worked? What has it produced?



sed 's/~/\n~/g' < filename > outputfile

i tried this to get a new line inserted into the file. i got 0 output, if i can get the tilde to become a new line then awk or grep will work.

This “works” if you can get the PO1’s on their own lines. You could do that with a sed statement.

find . -type f –name -exec grep -i -c "PO1" {} \; > a1
awk '{total += $1}; {print total}' a1 > a2
tot=`tail -1 a2`
echo $tot
rm a1 a2

Reply With Quote
  #4  
Old April 9th, 2008, 12:28 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 591 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 51 m 38 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
A file formatted as you specified (albiet a bit smaller)
Code:
<0> test : cat tilda 
this^is^a^separated^PO1^line~with^another^PO1^one^PO1^after^it~And^a^third
<0> test :

Counting teh number of "PO1" occurances in it
Code:
<0> test : tr '~' '\n' < tilda | tr '^' '\n' | egrep "PO1" | wc -l
3
<0> test : 

Caveat: If there are two PO1 items within a single field, they will not be counted twice. This essentially counts the number of fields in the file that contain the text "PO1". I figured that was ok considering your grep example.

Reply With Quote
  #5  
Old April 9th, 2008, 01:05 PM
rudek01 rudek01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 3 rudek01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 27 sec
Reputation Power: 0
got it thanks

for afile in `find . -type f -print`

do

tr '~' '\n' < $afile | tr '^' '\n' | egrep "PO1" | wc -l > countlines.txt

done

Reply With Quote
  #6  
Old April 10th, 2008, 10:53 PM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 95 ghostdog74 User rank is Private First Class (20 - 50 Reputation Level)ghostdog74 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 18 h 34 m 6 sec
Reputation Power: 3
Code:
# more file
this^is^a^separated^PO1^line~with^another^PO1^one^PO1^after^it~And^a^third
# more test.sh
#!/bin/sh
awk '{
n=gsub("PO1","")
print "number of times PO1 appears: "n
exit}
' file
# ./test.sh
number of times PO1 appears: 3

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > I have stumped 2 unix scripters already


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 5 hosted by Hostway