Noob needs script help Grep

   while read s1 s2; do
grep -i -w $s1 6-29data | tr "[A-Z]" "[a-z]" | sed 's/^[^,]*,//' | sed 's/200906/2009-06-/g' >> $s1.txt ;

cat header > here ;
cat $s1.txt | sort | uniq | tac >> here ;
cat here | uniq > $s1.txt ;

done < list 

Wow this almost worked file list has 8000 stock symbols which are updated by grepping the symbol in that days data for all symbols then writing to a text file.

list looks like this only much longer

wxs
wy
wye
wyn
x
xaa
xcj
xco
xec
xel
xel-a
xel-c
xfb
xfd
xfh
xfl

The problem is this there are stock symbols on nyse and amex with only one symbol for name such as X, Y, C,

here's what the data file 6-29data looked like today only much shorter.

XIN,20090629,6.8,6.87,6.45,6.46,186300
XJT,20090629,1.26,1.32,1.25,1.27,32600
XKK,20090629,6.59,6.59,6.55,6.57,4700
XKN,20090629,11.16,11.31,10.7,11.31,17300
XKO,20090629,9.81,9.81,9.81,9.81,1000
XL,20090629,10.87,11.75,10.76,11.49,6317400
XL-Y,20090629,19,19.8,19,19.6,7000
XOM,20090629,69.45,70.78,69.13,70.58,24359100
XRM,20090629,1.09,1.16,1.09,1.11,298300
XRX,20090629,6.65,6.71,6.52,6.62,3966800
XTO,20090629,38.5,38.83,37.81,37.98,7603400
XVF,20090629,13.24,13.35,13.24,13.35,5400
XVG,20090629,23.05,23.05,22.32,22.65,1300
Y,20090629,263.42,268.63,262.3,266.69,18500

The problem is this grep -i -w $s1 6-29data
works it finds incidents of one letter symbols like X,Y,C but it also grabs data from the hyphenated symbols like XL-Y ............

Hope this makes sense, sorry wasn't born with the coding gift perhaps you can help :slight_smile:

It's not clear to me exactly what you're trying to achieve here but to match the first field and then process the second, you'd be better of with awk IMO

while read s1 s2; do
nawk -F, 6-29data '{if ($1 == (tolower(symbol))) {sub("200906", "2009-06-", $2); print $0}}' symbol=$s1 >>$s1.txt
.
.
.

Sorry if not clear
grep -i -w $s1 6-29data

where s1=f

I want to find

F,20090629,5.66,5.96,5.6,5.78,73552200

not

MER-F,20090629,17.38,17.49,17.17,17.3,55600
PLD-F,20090629,15.5,15.6,15.11,15.48,2800
PSA-F,20090629,19.4,19.4,19.14,19.18,1900
RBS-F,20090629,13.89,14.22,13.75,14.02,16600
SFI-F,20090629,7.16,7.16,7.16,7.16,500

Try...

grep -i "^$s1," 6-29data 

if hyphen is the only concern, add a grep -v "-" after the 1st grep condition.

grep -i -w $s1 6-29data | grep -v "-" | tr "[A-Z]" "[a-z]" | sed 's/^[^,]*,//' | sed 's/200906/2009-06-/g' >> $s1.txt ;

Just a workaround and not a solution :smiley: