LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-08-2013, 11:42 AM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
question in a for loop


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
Attached Files
File Type: txt 20131001EMA10_num.txt (3.7 KB, 17 views)
File Type: txt 20131002EMA10_num.txt (3.9 KB, 15 views)
File Type: txt outfile.txt (3.1 KB, 14 views)
 
Old 10-08-2013, 12:21 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,101

Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by john83reuben View Post
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
 
Old 10-08-2013, 12:32 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,437

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
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
 
Old 10-08-2013, 12:58 PM   #4
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
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
 
Old 10-08-2013, 03:29 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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.
 
Old 10-08-2013, 03:42 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,456

Rep: Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844
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
 
Old 10-08-2013, 04:28 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,437

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
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.
 
Old 10-08-2013, 10:59 PM   #8
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
thanks all, i will try it out
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Infinite loop question. earthw0rmjim Programming 5 01-24-2013 12:30 PM
for loop question s_linux Programming 4 09-07-2011 06:26 PM
C programming Loop question? chibi2666 Programming 8 06-16-2009 09:15 PM
while loop question IceOner Programming 6 10-26-2007 08:23 AM
vbs loop question jonlake Programming 3 10-05-2006 05:32 PM


All times are GMT -5. The time now is 09:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration