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!

executable script

Status
Not open for further replies.

codemut

Programmer
Feb 29, 2008
26
US
Challenge: to convert what works on the command line into an executable script, putting each string within a file into an array, the file delimeter being empty space of any length.

This works on the command line:

gawk '/searchstring/ {split($0, arr, " "); for (i = 1; i <= NF; i++) { if (arr == "string") {print arr[i+1]}}}' file

This below doesn't work as the executable:

#!/usr/bin/gawk -f

BEGIN {
while ( getline < "file" ) {
split($0, arr, " ")
}
}

for (i = 1; i <= NF; i++) {
if (arr == "string") {
print arr[i+1]
}
}

}

Perhaps it needs to accommodate multidim arrays in the executable... but I'd like to keep it simple.
 
#!/usr/bin/gawk -f
BEGIN {
while(getline < "file"){
n=split($0,arr," ")
for(i=1;i<=n;++i)
if(arr=="string")
print arr[i+1]
}
}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

Your solutions are right on! So, can you recommend a favorite awk book? I have Effective Awk by Robbins. It would be nice to find more for accommodating user defined function libraries... maybe for a coming version of gawk?
 
I find the online gawk manual to be an excellent reference, with lots of examples.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top