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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk Print Question. Using printf with an array variable

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Help! I want to do something like the following. I want to be able to read certain fields of a file using awk and print them out using a formatted print statement. However, the part I'm struggling with is I want to use what is in field 2 of the file and then print the array element that matches that field. For example, if the second field of the first line of file temp2 was equal to 10, then I want to print the array variable dsa[10] instead of the actual field. Is this possible?? The stuff below does not work but I included for clarification. There are some workarounds such as reading in the file one line at a time but that is very very slow when dealing with hundreds of thousands of lines. Any help would be greatly appreciated.

Thanks,
KBH



# Script1 below
integer n=1
integer dsatype

while read dsa[n]
do
n=n+1
done < dsa.file


awk '{printf(&quot;%43s %14s %10s %10s\n&quot;, ${dsa[$2]},$3,$4,$5)}' temp2 > temp3
 
Something like this might work.

awk ' BEGIN {
while ((getline arr[a++] < &quot;filename&quot;) > 0) {
}
close(&quot;filename&quot;)
}

{
for (all in arr) {
if ($2 ~ all) {
print arr[all], &quot;matched&quot;, all, &quot;at&quot;, $2
}
}
}' filename | sort -u

Goodluck.
MMD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top