awk getline function help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk getline function help
# 1  
Old 04-02-2014
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 variable .

Below is the code

Code:
awk 'BEGIN{getline f;}NR%3==2{x="F"++i;a[i]=x;print f>x;}{print > x}END{for(j=1;j<i;j++)print> a[j];}'

Currently the getline function is only holding the first line of header file and I want it to hold all the 4 lines

Below is the sample input

Code:
HEADER
HEADER
HEADER
HEADER
 Unix
 Linux
 Solaris
 ABC
 DEF
 IGH
 Unix
 Linux
 Solaris

Want the output as

First file

Code:
HEADER
HEADER
HEADER
HEADER
 Unix
 Linux
 Solaris

Second file
Code:
HEADER
HEADER
HEADER
HEADER
 ABC
 DEF
 IGH

and so on

Please suggest
# 2  
Old 04-02-2014
Code:
awk 'BEGIN{getline f1;getline f2; getline f3; getline f4; f=f1 "\n" f2 "\n" f3 "\n" f4}NR%3==2{x="F"++i;a[i]=x;print f>x;}{print > x}END{for(j=1;j<i;j++)print> a[j];}'

# 3  
Old 04-02-2014
Thanks a lot however I am getting the below error

awk: A print or getline function must have a file name.

I have blank lines in file

Please let me know how to overcome that
# 4  
Old 04-02-2014
In the BEGIN section, there no file open yet, so you can't getline unless you redirect it.
Try
Code:
awk     'NR < 5         {HD[NR]=$0; next}
         !((NR-5)%3)    {FN="F"++i; for (j=1; j<=4; j++) print HD[j] > FN}
                        {print $0 > FN}             
        ' file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-02-2014
Quote:
Originally Posted by RudiC
In the BEGIN section, there no file open yet, so you can't getline unless you redirect it.
Try
Code:
awk     'NR < 5         {HD[NR]=$0; next}
         !((NR-5)%3)    {FN="F"++i; for (j=1; j<=4; j++) print HD[j] > FN}
                        {print $0 > FN}             
        ' file

Code:
$ cat file
hdr1
hdr2
hi
hello
$ awk ' BEGIN{ getline a;getline b } { print a b $0 } ' file
hdr1hdr2hi
hdr1hdr2hello
$

This User Gave Thanks to anbu23 For This Post:
# 6  
Old 04-02-2014
The getline is a tricky function to use and it can hold surprises. For example, if you use getline in the form getline f then this also changes the variable NR and FNR, so if you first do 4 getlines to get the header into variables or arrays, then when reading the first line after the headers, NR and FNR are equal to 5. This may, or may not be what you expect..

Last edited by Scrutinizer; 04-02-2014 at 12:03 PM..
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 question

hi experts i like to know how to print more line using getline command using awk. with below command i can only see one line (line no:1) however i do have more line as shown below line no:2,line no:3,line no:4 and so forth. how do i get those to show up using the below command. I tried... (7 Replies)
Discussion started by: Jared
7 Replies

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

get sysname with getline in awk

I am having issues getting this working. I am trying to get the system name and print it out in a awk script. I looked and looked, it looks like my syntax is correct but it is not working. What am I overlooking?? BEGIN { sys1 = "system( "uname -n" )" "sys1" | getline sys ... (2 Replies)
Discussion started by: timj123
2 Replies

8. Shell Programming and Scripting

NAWK getline function

Hi, I am using a Solaris OS here. My intention is to print the 2nd field if the first field matches "APPLE=". I am using the "getline" function here (shown below), but it doesn;t work. Can any experts give me some advice? Input: ORANGE= 10 APPLE= 20 MANGO= 30 GRAPES= 40 Output: 20 ... (5 Replies)
Discussion started by: Raynon
5 Replies

9. 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

10. Shell Programming and Scripting

awk:Problem with getline

$ echo |awk ' BEGIN {"date" | getline current_time;close("date");print "Report printed on " current_time}' Report printed on Thu May 11 14:57:29 METDST 2006 This example works fine but how can i print all the output when is longer... (3 Replies)
Discussion started by: Klashxx
3 Replies
Login or Register to Ask a Question