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

Hi guys, Some help with AWK would be appreciated

Status
Not open for further replies.

GlenA

Programmer
Apr 21, 2003
24
GB
Hey, it's not urgent but i would really like to have this working.

We just got our new linux server in the office and we were previously working on NT. With NT i could extract files into a comma dilimited form and then just search the records, whereas with Linux i'm having a few problems, i've asked a few people around and they said that with AWK i should be able to extarct all that i need from a text file.

Well, here's the question: would that be possible to write an AWK application that would display and extarct entries according to the first character of the field? So for every line of input in a file it will find words starting with "N or #". Count the number of letters/digits in each of these words and at the end print out the average number of letters per word beginning with "N or #"?


Thank you
 
yes, it would. Use 'match' and associative arrays

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I believe i 've found an easier solution, unfortunately only for a first bit

awk '$0 ~ /[N]/{
for (i = 1; i <= NF; i++) {
if ($i ~ /^[N]/) {
print $i, length($i)
}
}
}'




how do i go about: at the end of output, print out the total number of letters (for words starting with N and the average number of letters per words beginning with &quot;N&quot;?.

Thanks
 
Something like this.

awk '$0 ~ /[N#]/{
for (i = 1; i <= NF; i++) {
if ($i ~ /^[N#]/) {
print $i, length($i)
len += length($i)
k++
}
}
}
END{
if (k) print &quot;ave = &quot; len/k
}'


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top