awk - getline from parametric file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - getline from parametric file name
# 1  
Old 06-15-2010
awk - getline from parametric file name

Hi!
I have an input file for an awk script that I need to split into several files and the process them separately line by line.
I have splitted the input file into the other files, that have been created correctly. But, since their names are parametric (i.e. output_1.txt, output_2.txt.. output_3.txt), I have some problems in reading them line by line using getline. Here is my code

Code:
RS="new";
FS="\n";
    while (getline < ARGV[1] ){
            if (NF > 1){ 
                print $line > "output_"i".txt"
                close ("output_"i".txt");
            }
            i++;
    }
    numSamples=i;
    #process the single files created
    RS="\n";
    FS="  ";
    j=0;
    for (j=0;j<numSamples;j++){
        file="output_"j".txt"
        getline < ($file)   #getline here cannot find the files
        print $line
        #then process a single line of the file I am reading...
    }

The input file is in the format:

HTML Code:
new
  900.00    0   879.58   998.00 
  900.00    1  1999.00   998.00 
  900.00    2   817.38  1002.00 
  900.00   12   687.10   998.00 
  900.00   13     6.67  1002.00 
  900.00   14   287.47  1002.00 
  900.00   15  1317.82   998.00 
  900.00   16   531.71  1002.00 
new
  936.85    0  1695.27   998.00 
  936.85    1  1736.01  1002.00 
  936.85    2     1.00  1002.00 
  936.85    3  1431.56   998.00 
  936.85    4  1999.00   998.00 
  936.85    5  1993.92   998.00 
  936.85    6  1965.63   998.00 
  936.85    7  1556.24  1002.00 
  ...
Any idea on alternatives to getline in reading line by line the different files? What is wrong with my code? Thank you very much!!
# 2  
Old 06-15-2010
Hi Alice236,

Welcome to this forum. There is no need to use getline. I am not sure if I fully understand why you would want to split the file and then subsequently read them again.. What is your desired end result?

As an example, you can split the input file without getline into several files like so:
Code:
awk 'NF==1{i++;next}{print>"output_"i".txt"}' infile

You could read and process those files like so:
Code:
awk '{print; process ...}' output_*.txt

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-15-2010
I need to split into several files because, apart from awk, I use them also for plotting some statistics.
I tried to use getline to read them because I didn't have any better idea (I am still trying to learn awk and I need a lot of practice, as you can see.. ;-))
Anyway.. thank you very much for your solution, you saved my thesis for today! :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with if, getline, and another if

Howdy Folks, It seems like it is always awk that confuses the heck out of me and I even have books and examples. I have this line: awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) getline; if ($2 ~ /r100595/) {print SRV,"BUNDLE-GIM",$2}}' post.txt to parse this text: <api... (4 Replies)
Discussion started by: port43
4 Replies

2. Shell Programming and Scripting

awk getline

Hi, I have an awk script with the following function in it . function cmd( c ) { while( ( c | getline foo) > 0 ){ return foo ; close( c ); } } c =... (4 Replies)
Discussion started by: MetaMan
4 Replies

3. Shell Programming and Scripting

awk getline function help

Hello All , I have to split a file as well as keep the header in all the splitted files. For this I am using the getline function of awk to keep the header however the catch is header is of 4 lines and I have to hold all the 4 lines by getline function(or is there any other option ???) into a... (5 Replies)
Discussion started by: Pratik4891
5 Replies

4. Shell Programming and Scripting

awk getline t file

I want to import a textfile with getline into var t which has several lines. How do import all lines, since it only imports the last line: while < ((getline t "textfile") > 0) (7 Replies)
Discussion started by: sdf
7 Replies

5. Shell Programming and Scripting

awk print header as text from separate file with getline

I would like to print the output beginning with a header from a seperate file like this: awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ ) print }" input.txtWhat am i doing wrong? (4 Replies)
Discussion started by: sdf
4 Replies

6. Shell Programming and Scripting

Some Awk Getline help?

Greetings, I have about 3000 files that I want to search. The first column in all of these 3000 files has a unique serial number on each line. The subsequent columns have lots of data. I have another masterfile with three columns to help me find all the data I need in a moments notice: col 1... (15 Replies)
Discussion started by: jeeplou
15 Replies

7. Shell Programming and Scripting

Using getline in awk

I am using awk and want to use getline from a file like below getline x < file However file consists of two columns and I only want to store $2 Any way I can do this? ---------- Post updated at 06:54 AM ---------- Previous update was at 06:45 AM ---------- Done something like this.... (1 Reply)
Discussion started by: kristinu
1 Replies

8. Shell Programming and Scripting

awk getline

How do you make the getline function return to the original line? The example below should make it clear where I am currently going wrong. Thanks AWK SCRIPT: ------------- awk -F '-' '{ tmpLine = "EMPTY" print "CURRENT LINE :"$0 getline tmpLine print "NEXT LINE :"tmpLine }'... (1 Reply)
Discussion started by: garethsays
1 Replies

9. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

10. Shell Programming and Scripting

awk getline help maybe?

hello collegues, I am attempting to use awk to search file1 (serverlist.csv) from each row with file2 (supported.txt). If the is no entry exists in serverlist then output to a file called notsupp.out if there is an entry output to supp.out I can do this with basic shell scripting however... (0 Replies)
Discussion started by: chlawren
0 Replies
Login or Register to Ask a Question