|
|
|
| |||||||||
![]() |
|
|
«
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
|
|||
|
|||
|
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?
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
got it thanks
for afile in `find . -type f -print`
do tr '~' '\n' < $afile | tr '^' '\n' | egrep "PO1" | wc -l > countlines.txt done |
|
#6
|
|||
|
|||
|
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
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > I have stumped 2 unix scripters already |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|