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!

How to create my own function in awk? 2

Status
Not open for further replies.

tikual

Technical User
Jun 10, 2003
237
HK
I have created a function in my awk script as follows:

function fun_display() {
for (i=1;i<=PageOne;i++)
if (menupage ~ /^A/)
print substr(menupage,3)
}
I use it to display the line when it starts from &quot;A&quot; letter.

After reading my learning book, it shown that I can insert few variables in ( ) sign. So I tried as follows:

function fun_display(letter) {
for (i=1;i<=PageOne;i++)
if (menupage ~ /^letter/)
print substr(menupage,3)
}

But it didn't work. So how to make my own function? Do anyone can give me an example?

Another question, where can I find awk script example from web? Because I really confused in field one &quot;$1&quot; and the shell parameter &quot;also $1&quot;. When I tried to write my awk script, I would not sure my script treats it as field one or shell parameter.

Thanks!


tikual
 
Try this:code
function fun_display(letter ,i) {
for (i=1;i<=PageOne;i++)
if (menupage ~ &quot;^&quot;letter)
print substr(menupage,3)
}[/code]
Note: The i isn't a parameter but a local variable

Hope This Help
PH.
 
Sory for the typo
Try this:
Code:
function fun_display(letter  ,i) {
for (i=1;i<=PageOne;i++)
if (menupage ~ &quot;^&quot;letter)
print substr(menupage,3)
}
Note: The i isn't a parameter but a local variable

Hope This Help
PH.
 
Thanks PHV,

But I don't know how to use it. I tried as follows:

Actually I call this function in BEGIN{} once. So the original one is
BEGIN {
.
.
fun_display()
}

Then I use your code but nothing to be printed. And now my code is like these:
.
.
fun_display(i)
}

i=A <---is it necessary??
function fun_display(letter ,i) {
for (i=1;i<=PageOne;i++)
if (menupage ~ &quot;^&quot;letter) <--why use &quot;^&quot;, mine not work?
print substr(menupage,3)
}

what's wrong to me?? I can print the line start from letter &quot;A&quot; if I don't use variable. Just want to know how to assign variable. Thanks again!!

tikual
 
function fun_display(letter ,i) {
for (i=1;i<=PageOne;i++) # <- Why this loop ?
if (menupage ~ &quot;^&quot;letter)
print substr(menupage,3)
}
BEGIN {
.
.
fun_display(&quot;A&quot;)
# Other way
Initiale=&quot;A&quot;
fun_display(Initiale)
}

Jean Pierre.
 
And this ?
Code:
function fun_display(letter  ,i) {
   for (i=1;i<=PageOne;i++)  # <- Why this loop ?
      if (menupage[i] ~ &quot;^&quot;letter)
         print substr(menupage,3)
}
BEGIN {
   .
   .
   fun_display(&quot;A&quot;)
}

Hope This Help
PH.
 
ok, let all of you see my draft script first.

#!/bin/awk -f
BEGIN {
FS=&quot;!!&quot;
while ((getline < &quot;menu.list&quot;) > 0){
++PageOne
menupage[PageOne]=$0
++count[$1]
}
fun_display()
}

function fun_display() {
for (i=1;i<=PageOne;i++)
if (menupage ~ /^A/)
print substr(menupage,3)
print &quot;&quot;
printf &quot;Enter your choice: \n&quot;
}


The following is my menu list.

A List Local Information -->
A List Network Information -->
A List Status -->
A Quit
B Local Information Page -->
B List Hostname!!uname -a
B List HostID!!hostid
B View VMstat!!vmstat
B View IOstat!!iostat
B View Swap Space!!free
B View Error Logs!!dmesg
B View User List!!more /etc/passwd
B View User Group!!more /etc/group
B Backup to Main Page

When I try the code as:

.
.
fun_display()
}

function fun_display() {
for (i=1;i<=PageOne;i++)
if (menupage ~ /^A/)
.
.
.

Result:
List Local Information -->
List Network Information -->
List Status -->
Quit

Enter your choice:

But, if I use both of your suggestion, the result is:

Enter your choice:

No list found. Anyway, I will try again when I back to home. Thanks all!!

tikual
 
Code:
#!/bin/awk -f
BEGIN {
FS=&quot;!!&quot;
while ((getline < &quot;menu.list&quot;) > 0){
        ++PageOne
        menupage[PageOne]=$0
        ++count[$1]
}
fun_display(&quot;A&quot;)
}

function fun_display(letter  ,i) {
  for (i=1;i<=PageOne;i++)
    if (menupage[i] ~ &quot;^&quot;letter)
      print substr(menupage,3)
  print &quot;&quot;
  printf &quot;Enter your choice: \n&quot;
}

Hope This Help
PH.
 
Note you can do the same thing without awk:

grep '^A' menu.list | cut -c3-
echo &quot;\nEnter your choice : &quot;

It's possible to define a function in your script shell (sh, bash, ksh) :

DisplayMenu() {
grep &quot;^${1:-A}&quot; menu.list | cut -c3-
echo &quot;\nEnter your choice :&quot;
}

If you want to display a menu :

DisplayMenu # Display default Menu &quot;A&quot;
DisplayMenu A # Menu A
DisplayMenu B # Menu B

Jean Pierre.
 
Thanks both of your non-stop support! And now I know that /^.../ not works in 'if' condition.

Aigles, thanks for your reminder. Just self-learning of awk to do the menu.

Both got a star!

tikual
 
Hey PHV,

Congratulations on being chosen Tipmaster of the Week!

CaKiwi
 
PHV,
congrats and keep on postin'!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
PHV the tip-master of the week,
Your posts are enlightening and your follow-up on the questions is very much appreciated.
Félicitations.
 
Hi PHV,

Toutes mes félicitations pour cette distinction méritée.

Jean Pierre.
 
Thanks all guys.
I'm very glad to have the ability to help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top