LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   question in a for loop (https://www.linuxquestions.org/questions/programming-9/question-in-a-for-loop-4175480043/)

john83reuben 10-08-2013 11:42 AM

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
I am abit confused on the output file (outfile.txt).
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

danielbmartin 10-08-2013 12:21 PM

Quote:

Originally Posted by john83reuben (Post 5042201)
I am a bit confused on the output file (outfile.txt).

Please give an explanation of what you are trying to do.

Daniel B. Martin

rknichols 10-08-2013 12:32 PM

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

john83reuben 10-08-2013 12:58 PM

Yes, your right. I did as below.

Code:


for i in `cat 20131001EMA10_num.txt | awk -F" " '{print $2}'` ; do grep -w $i  20131002EMA10_num.txt  >> outfile.txt  ; done

Thanks for the help

John

PTrenholme 10-08-2013 03:29 PM

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.

ntubski 10-08-2013 03:42 PM

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

rknichols 10-08-2013 04:28 PM

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
The advantage here is that each file is read exactly once, rather than reading through your 2nd file for each line in your first.

john83reuben 10-08-2013 10:59 PM

thanks all, i will try it out


All times are GMT -5. The time now is 06:39 AM.