Sunday, June 22, 2014

awk: User Controlled Input(1)

1. User Controlled Input -- from standard input
text2:
 Hello Chicago  
 Hello Los Angeles  
 Hello Boston  
 Hello Atlantic  

script_1:
 #! /bin/bash  
   
 awk 'BEGIN {   
   print $0;  
 #output: empty line;  
 #At this time, $0 is still empty  
   
   getline;  
 #Read in the next record from standard input to  
 #$0, while updating the NF NR at the same time.  
   
   print "BEGIN:" $0;  
   print "BEGIN: NF, NR:", NF, NR;  
 #output:  
 #BEGIN:Hello Chicago  
 #BEGIN: NF, NR: 2 1  
   
   getline v;  
 #Read in the next record from standard input to  
 #variable v, while updating the NF, BUT NOT UPDATING  
 # NF, since $0 is not touched at all.  
   
   print "BEGIN: v:",v;  
   print "BEGIN: NF, NR:", NF, NR;  
 #output:  
 #BEGIN: v: Hello Los Angeles  
 #BEGIN: NF, NR: 2 2  
 #Actually the value of NF is wrong, number of fields is  
 #supposed to be 3 in this case. But since $0 is not touched  
 #so it does not get updated.  
 }  
 { print $0; }  
 #Since BEGIN section already read in 2 lines of records  
 #this action will be applied on remaining records only  
 ' text2  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
   
 BEGIN:Hello Chicago  
 BEGIN: NF, NR: 2 1  
 BEGIN: v: Hello Los Angeles  
 BEGIN: NF, NR: 2 2  
 Hello Boston  
 Hello Atlantic  

2. User Controlled Input -- from external file
text:
 Hello world!  
 Hello Amazing world!  

text2:
 Hello Chicago  
 Hello Los Angeles  
 Hello Boston  
 Hello Atlantic  

script_1:
 #! /bin/bash  
   
 awk 'BEGIN {   
   getline <"text";  
 #Since this is our first time to read from "text" file,  
 #awk will open the file specifically and do all necessary  
 #underlying work.  
 #Similarly, getline will read the input record from "text"  
 #file to $0, and NF, NR get updated.  
   
   print "BEGIN:" $0;  
   print "BEGIN: NF, NR:" NF, NR;  
 #Output:  
 #BEGIN:Hello world!  
 #BEGIN: NF, NR:2 0  
   
   close("text");  
 #This will close the "text" file, then next getline  
 #command will re-open the "text" file, and make reading  
 #position to go to the beginning of file again.   
 #Without closing the file here, next getline command  
 #will read in "next record" compared to the one it   
 #already read in above.  
   
   getline v<"text";  
 #Since we already closed the "text" file above, in this  
 #case, getline has to re-open it. And start reading into  
 #record from the beginning again. Similarly because getline  
 #read into variable v, instead of $0, NF is not getting updated.  
   
   print "BEGIN:" v;  
   print "BEGIN: NF, NR:" NF, NR;  
 #Output:  
 #BEGIN:Hello world!  
 #BEGIN: NF, NR:2 0  
 }  
 { print $0; }  
 #In Begin section, we do not touch the standard input.  
 #So this action will be applied on all records.  
 ' text2  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 BEGIN:Hello world!  
 BEGIN: NF, NR:2 0  
 BEGIN:Hello Amazing world!  
 BEGIN: NF, NR:2 0  
 Hello Chicago  
 Hello Los Angeles  
 Hello Boston  
 Hello Atlantic  

No comments:

Post a Comment