For loop using 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers For loop using 2 files
# 8  
Old 02-02-2009
You are correct in the code above...

and because of the second false condition, we would print nothing and move onto the second line in file2, where 38>=30 (true) AND 41<=44 (true) hence here we would print 30,44(file1) and then int this way we would continue to loop file2 until the end
# 9  
Old 02-02-2009
Do you get the desired result with the following code?

Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk):

Code:
awk '(getline f2 < F2) > 0 {
  split(f2, t)
  if (t[1] >= $1 && t[2] <= $2)
    print 
    }' F2=file2 file1

# 10  
Old 02-02-2009
Basically, we would would be doing this conditional check for each line in file2 against each line in file1. I have something like this:

Code:
awk -F, '\
BEGIN {
while ((getline < "file2") > 0)
file2[$2]=$3
}
{for (col1 in file2)
if ($0>=30 && $1<=44)
print $0} ' FILE1

but here is where I am stuck: I have hard-coded the number 30 and 44..this should refer to column1 and column 2, from file1 and print from FILE1 if the conditional statement is true.

Cheers for any solutions

Last edited by radoulov; 02-02-2009 at 07:48 AM.. Reason: added code tags
# 11  
Old 02-02-2009
Try the solution above and let me know.
# 12  
Old 02-02-2009
Maybe something like this (not tested):

Code:
awk 'NR==FNR{a[++i]=$1;b[i]=$2;next}
{ for(j=1;j<=i;j++){
   if(a[j] >= $1 && b[j] <= $2{print;break}
  }
}' file2 file1

Regards
# 13  
Old 02-02-2009
Thankyou to both of you. Both work well..but I would like to take a minute to understand the code

Code:
awk '(getline f2 < F2) > 0 {
  split(f2, t) # you split the columns in File 2?  t means tab delimiter?
  if (t[1] >= $1 && t[2] <= $2) # How does the code know that $2 and $1 come from file1
    print 
    }' F2=file2 file1


Last edited by radoulov; 02-02-2009 at 08:31 AM.. Reason: added code tags
# 14  
Old 02-02-2009
Quote:
you split the columns in File 2? t means tab delimiter?
From the GNU awk manual:

Quote:
split(string, array [, fieldsep])
This function divides string into pieces separated by fieldsep and stores the
pieces in array. The first piece is stored in array[1], the second piece in
array[2], and so forth. The string value of the third argument, fieldsep, is a
regexp describing where to split string (much as FS can be a regexp describing
where to split input records). If fieldsep is omitted, the value of FS is used.
t is the array name, fieldsep defaults to the current value of FS.

Quote:
How does the code know that $2 and $1 come from file1
Because awk reads the files in parallel,
in the block above both $0 (the current record from file1 and f2 (the current record from file2 via getline) are available.

Hope this helps.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loop over files and use awk

Hi, I have a large number of files which are numbered numerically, i.e. of the type 1.usr, 2.usr, 3.usr ... This is what I'd like to do: 1. In ascending order, use awk to read a value from each file. 2. Write this value to another file (say data.txt). This file, 'data.txt' should be... (4 Replies)
Discussion started by: lost.identity
4 Replies

2. Shell Programming and Scripting

awk_Compare two files with a loop

Hi, I have a small query when comparing two files with awk. I have a small piece of code running in a shell. See below: gawk -F"," 'NR == FNR { A=1; next } \!A' OFS="," 2011.csv 2012.csv > diff_2012.csv The code works fine (Note I had to escape the ! with \! to run in shell). What I want... (6 Replies)
Discussion started by: theflamingmoe
6 Replies

3. Linux

rename files using loop with different name

Hi, i need to write a shell script where i have to loop through all the file in a directory and rename them based on below condition. file1.dat file2.dat file3.dat the above files has to be moved to another directory like below file1_201001.dat file2_201002.dat file3_201003.dat... (3 Replies)
Discussion started by: feroz
3 Replies

4. Shell Programming and Scripting

Loop to copy like files

Hi, I need to write a script that copies all .zip files in the subdirectories of ~100 folders. No clue how to write a loop that goes into each folder, searches for a .zip file, and copies it and extracts it to a unique location. I imagine something like cp -f /home/folder1/*.zip... (6 Replies)
Discussion started by: nez
6 Replies

5. Shell Programming and Scripting

loop through files in directory

hi all i have some files present in a directory i want to loop through all the files in the directory each time i loop i should change the in_file parameter in the control file and load it into a table using sql loader there is only one table where i have to load alll the files ... (3 Replies)
Discussion started by: rajesh_tns
3 Replies

6. Shell Programming and Scripting

Grep Different Files Using a Loop?

I have a script to GREP for a text expression within certain files, the files being named file.11012008 thru file.11302008. 30 files in all, one for each day of the month. Instead of entering the following 3 lines of code 30 different times, I'm trying to find a way to loop the process: ... (6 Replies)
Discussion started by: foleyml
6 Replies

7. Shell Programming and Scripting

reading from 2 files through while loop

hi i have two files cat input.txt 123456| 43256 456482|5893242 cat data.txt xv 123456 abcd dsk sd 123456 afsfn dd df 43256 asdf ff ss 456482 aa sf 5893242 ff ff aa 5893242 aa aa i need to read inputs from input.txt and find data for data.txt. then i need to print them as a... (2 Replies)
Discussion started by: windows
2 Replies

8. UNIX for Dummies Questions & Answers

Loop through files

Hi, I'm trying loop through all files in a directory that have a filename starting with 'CC', and process them one by one. Can any provide an example of how I could do this. I've started with: if test -f CC* then #add files to an array #loop through array and process the file based on... (1 Reply)
Discussion started by: kshelluser
1 Replies

9. Shell Programming and Scripting

Loop through files in a directory

Hi, I want to write bash script that will keep on looking for files in a directory and if any file exists, it processes them. I want it to be a background process, which keeps looking for files in a directory. Is there any way to do that in bash script? I can loop through all the files like... (4 Replies)
Discussion started by: rladda
4 Replies
Login or Register to Ask a Question