Reading a file several times with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a file several times with awk
# 1  
Old 04-04-2010
Reading a file several times with awk

Hi everyone,

I was wondering if it's possible to read a file ("file2" in my example) more than once. In this example I want to print file2 entirely for each lines of file1:

Code:
awk -F$'\t' '{
    print $0
    while ((getline < "file2") > 0) {
        print "\t"$0
    }
}' file1

It obviously doesn't work as is (only prints file2 for the first line), but I was hoping for a way to somehow "reinitialize" the getline cursor?

That could be very helpful for me!
Thanks Smilie
Anthony
# 2  
Old 04-04-2010
After the while loop, close the filehandle:
Code:
close "file2"

Regards,
Alister
# 3  
Old 04-04-2010
Code:
awk -F'\t' '{
    print
    while ((getline < "file2") > 0) {
        print "\t" $0
    }
    close ("file2")    
}' file1



---------- Post updated at 10:13 PM ---------- Previous update was at 10:10 PM ----------

Or:

Code:
awk -F'\t' 'NR == FNR {
  f2 = f2 ? f2 RS OFS $0 : OFS $0
  next 
  }
{ print $0 RS f2 }
' OFS='\t' file2 file1



---------- Post updated at 10:18 PM ---------- Previous update was at 10:13 PM ----------

Quote:
Originally Posted by alister
After the while loop, close the filehandle:
Code:
close "file2"

Regards,
Alister
I didn't know that there was an awk implementation that accepts that syntax (close with no parenthesis) and just found that AT&T awk doesn't complain (gawk does).
# 4  
Old 04-04-2010
Brilliant, thanks a lot to both of you!
# 5  
Old 04-04-2010
Quote:
Originally Posted by radoulov
I didn't know that there was an awk implementation that accepts that syntax (close with no parenthesis) and just found that AT&T awk doesn't complain (gawk does).
I wasn't either (or maybe I was, subconsciously Smilie). I meant to use the usual function calling syntax with the parenthesis.

Nice catch, radoulov.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issues in reading file using 'awk'

Dear all, I am using following function of some script to assign variable "JobNo" some value form file $SAMPLE"_status.log" ( generated using the red color command ) crab ntuplize_crab -status -c $SAMPLE >& $SAMPLE"_status.log" & echo $SAMPLE"_status.log" "=====" jobNo=$(awk... (10 Replies)
Discussion started by: emily
10 Replies

2. Shell Programming and Scripting

Reading data from file using awk

I have a file as below. It contains two data sets separated by >. I want to pipe each data set to another program called psxy. How can I get the different records Have started doing as follows but it only passes the first data set awk 'BEGIN {RS=">"};{print $0}' p.dat cat p.dat... (12 Replies)
Discussion started by: kristinu
12 Replies

3. Shell Programming and Scripting

awk issue while reading from file in while do

Hi Friends, I am trying to scan line by line using awk and pull the values and pass it in variables and then will use the variables but doesn't work. Please see below for details. #more dbtest.sh ---------------------------------- #!/bin/bash . $HOME/.bash_profile while read line do... (6 Replies)
Discussion started by: narunice
6 Replies

4. Shell Programming and Scripting

reading file awk or while

While read line query !!! Folks, I am working on a file which has entries as follows. I am using while read line to generate desired output as follows. filename1: Name : sdt2156157_ID NOS : 4567 NOS : 2348 Name : sdt2156158_ID NOS : 4987 NOS :... (3 Replies)
Discussion started by: dynamax
3 Replies

5. Shell Programming and Scripting

awk file reading doubt

Hi, Using this trivial code, I am trying to insert/paste the single column data of a file into the second column (field 2) of a multi-column text file. awk 'FNR==NR {a=$0; next} {$1=$1 OFS a}1' single-column-file multi-column-file Lets consider the single-column-file as 'f2' and multi-column... (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. Shell Programming and Scripting

awk- reading input file twice

Hello, I've been trying to come up with a solution for the following problem; I have an input file with two columns and I want to print as an output the first column without any changes but for the second column, I want to divide it by its last value. Example input: 1 9 2 10 3 11 4 12 5... (14 Replies)
Discussion started by: acsg
14 Replies

7. Shell Programming and Scripting

Reading passwd and need to use it multple times when script asks

Hi Gurus, I have one requirment.. I have written a script and it asks a registry passwd while performing some clearcase command. Now we are giving it manually. It's for one time run. We want to perform this for multiple times on multiple files throguh for loop.. means we need to pass the same... (3 Replies)
Discussion started by: raghu.iv85
3 Replies

8. Shell Programming and Scripting

How to make AWK process an input file many many times?

By "many many times" I mean the times the input file is to be processed is unknown beforehand, it will be known when awk finishes processing the input file for the first time. So my question is: how to start over again from the first record of the input file when AWK finishes processing the... (7 Replies)
Discussion started by: kevintse
7 Replies

9. Shell Programming and Scripting

Using awk to when reading a file to search and output to file

Hi, I am not sure if this will work or not. I am getting a syntax error. I am reading fileA, using an acct number field trying to see if it exists in fileB and output to new file. Can anyone tell me if what I am doing will work or should I attempt it another way? Thanks. exec < "${fileA}... (4 Replies)
Discussion started by: ski
4 Replies

10. Shell Programming and Scripting

Reading large file, awk and cut

Hello all, I have 2 files, the first (indexFile1) contains start offset and length for each record inside the second file. The second file can be very large, each actual record start offset and length is defined by the entry in indexFile1. Since there are no records separators wc-l returns 0 for... (1 Reply)
Discussion started by: gio001
1 Replies
Login or Register to Ask a Question