Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getline to populate an array: skipping blanks

Status
Not open for further replies.

karenlu

MIS
Feb 25, 2010
2
US
I am using the getline x < file in the BEGIN{} section of an awk script to read an auxiliary file and populate an array which will be used to supply values in the main loop. The auxiliary files have inconsistent formats -- both " " and tab delimiters and arbitrary numbers of blank lines. In order to assign the correct index numbers to my created array I need to ignore blank lines within the getline loop but this has proved difficult. This code:
BEGIN {
...
cnum=1;
while (getline c < file > 0) {
if (c ~ /^$/ || c == " " || c == "") {
print "Blank line"
} else {
split(c,ca)
array[cnum]=ca[1]
cnum++
}
}
close(file);
...
}
Still seems to count the blank lines and increments the index cnum.
I need to be able to deal with arbitrary spacing so I can't simply adjust the index.
Any suggestions welcome.
This is awk under linux FC12 so I assume it is == Gawk.
 
Try:

Code:
        if (c ~ /^[[:space:]]*$/) {

The [:space:] character class includes spaces, tabs, etc, so that regular expression should match a line containing any number of those, and nothing else.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top