question in a for loop
3 Attachment(s)
Hi All,
Code:
for i in `cat 20131001EMA10_num.txt | awk -F" " '{print $2}'` ; do awk /$i/ 20131002EMA10_num.txt >> outfile.txt ; done Based on outfile.txt, why LEESK is in the 23rd line/row and not in line/row 13th as per 20131001EMA10_num.txt. Because the for loop is based on cat 20131001EMA10_num.txt, so i was expecting LEESK to be on line 13th in the outfile.txt. Thanks in advance John |
Quote:
Daniel B. Martin |
In that 2nd awk command, your match on "$i" is not restricted to whole words, so "AT" in particular makes a lot of matches. Try this:
Code:
for i in `cat 20131001EMA10_num.txt | awk -F" " '{print $2}'` ; do awk '/\<$i\>/' 20131002EMA10_num.txt >> outfile.txt ; done |
Yes, your right. I did as below.
Code:
John |
You might consider simplifying you code to
for i in $(cut -d\ -f2 20131001EMA10_num.txt);do grep -w $i 20131002EMA10_num.txt >>outfile.txt;done although any speed improvements would probably be unnoticeable unless you do a lot of these. In any case cating a file into awk is silly, since awk can open and read on its own, Viz: `cat 20131001EMA10_num.txt | awk -F" " '{print $2}'` and `awk -F" " '{print $2}' 20131001EMA10_num.txt` should produce identical results. |
If you don't care about the order in outfile.txt you could simplify it further (this is noticeably faster):
Code:
cut -d\ -f2 20131001EMA10_num.txt | grep -wFf - 20131002EMA10_num.txt > outfile.txt |
If the files are large, you can make the operation much faster by trading memory for speed and reading the entire 2nd file into an awk associative array. Note that your 2nd file is now the first file argument to awk:
Code:
awk 'ARGIND==1 { list2[$2] = $1; next } { if($2 in list2) print list2[$2], $2 }' 20131002EMA10_num.txt 20131001EMA10_num.txt |
thanks all, i will try it out
|
All times are GMT -5. The time now is 06:39 AM. |