grep/awk on a single column in a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep/awk on a single column in a for loop
# 1  
Old 12-01-2010
grep/awk on a single column in a for loop

Hi I'm trying to loop through a small list of id's and then pull out a few columns if the id matches that found in column 2 of the larger file. I managed to get one command to work
Code:
awk -F "   " '{if ($2 == '154080196') print $2,$3,$4}' tst.txt | less

However, when I try it in a for loop I get a problem. Is it because I have the two awk statements?
Code:
for i in `awk '{print $1}' keyIndvs.txt`; do echo $i; awk -F "    " '{if ($2 == $i) print $2,$3,$4}'  tst.txt |less

Any help would be appreciated...

Last edited by Scott; 12-01-2010 at 06:53 PM.. Reason: Please use code tags
# 2  
Old 12-01-2010
What problem do you get? Be specific.

"for x in `stuff`" is a useless use of backticks, wasteful, and also dangerous because strings in a shell often have a maximum size. Whenever you have that you can do it more safely and more efficiently like:

Code:
awk '...' | while read VALUE
do
        
done

...but you don't even need a loop. You can just feed one instance of awk into another with awk '{...}' | awk '{...}' ...but the best option would probably be to do it all in one instance of awk. It's a powerful enough language.
# 3  
Old 12-01-2010
I guess there are two files: keyIndvs.txt, tst.txt

For first file, you need the column 1 data.

For second file, you need find out the record with same in first file.

If my guess is right, all your script can be replaced by one awk command:
Code:
awk 'NR==FNR{a[$1];next} $2 in a {print $2,$3,$4}' keyIndvs.txt, tst.txt

second, if you need use system var in awk, try this:
Code:
awk -v var=$i '{if ($2 == var) print $2,$3,$4}'  tst.txt

# 4  
Old 12-02-2010
Thanks for both your replies. Sorry about the lack of detail, but rdcwayx was spot on with what I was attempting. Also thanks for the reference to the useless backticks. I'm trying to teach myself how to program and am bound to make some mistakes. I first tried the example:

awk 'NR==FNR{a[$1];next} $2 in a {print $2,$3,$4}' keyIndvs.txt, tst.txt

but nothing printed to screen and I'm still not 100% what FNR etc was doing so I tried to do the following:

awk '{print $1}' keyIndvs.txt | while read i; do echo $i; awk -F ' ' '{if ($2 == $i) print $2,$3,$4}' tst.txt;done

However, I can't get anything to work and I really don't have a clue why.

In the end I've decided to use R to solve my problem as I'm a bit more familiar with it. I was hoping I could use awk to do the job as the files I have to load into R are huge and it really takes a long time... Anyway here's the code I used if anyone is interested

>R
>indv = read.table('keyIndv.txt',header=FALSE)
>dat = read.table('tst.txt',header=FALSE)
>outF=subset(dat,(V2 %in% indv[,1]))
>write.table(outF,'keyDat.txt')

Thanks again for your help, unfortunately I had to go to R for my solution, but I picked up some awk commands and lessons that may come in handy...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

2. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

3. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

4. Shell Programming and Scripting

Awk multiple lines with 4th column on to a single line

This is related to one of my previous post.. I have huge file currently I am using loop to read file and checking each line to build this single record, its taking much much time to parse those records.. I thought there should be a way to do this in awk or sed. I found this code in this forum... (7 Replies)
Discussion started by: Vasan
7 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

7. Shell Programming and Scripting

grep/awk column or variable?

Hi, I'm running via PuTTY, in a BASH shell to do my work. I'm running calculations where steps are reported like this every 100 steps: NSTEP = 249900 TIME(PS) = 249.900 TEMP(K) = 299.94 PRESS = 21.1 Etot = -12912.5557 EKtot = 4996.8780 EPtot = -17909.4336 ... (6 Replies)
Discussion started by: Oriksagtaria
6 Replies

8. Shell Programming and Scripting

Looking for AWK Solution for column comparison in a single file

- I am looking for different kind of awk solution which I don't think is mentioned before in these forums. Number of rows in the file are fixed Their are two columns in file1.txt 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 I am looking for 3... (1 Reply)
Discussion started by: softwarekids23
1 Replies

9. Shell Programming and Scripting

Awk multiple lines with 3rd column onto a single line?

I have a H U G E file with over 1million entries in it. Looks something like this: USER0001|DEVICE001|VAR1 USER0001|DEVICE001|VAR2 USER0001|DEVICE001|VAR3 USER0001|DEVICE001|VAR4 USER0001|DEVICE001|VAR5 USER0001|DEVICE001|VAR6 USER0001|DEVICE002|VAR1 USER0001|DEVICE002|VAR2... (4 Replies)
Discussion started by: SoMoney
4 Replies

10. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies
Login or Register to Ask a Question