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!

Call A function 3

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

My problem is calling a function and if my function is correct.

Firstly I read a file for e.g.
(011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866 )
use $6,$7,$8,(*/*/*tedo:04.df23*403866*), search for that file (getline) and then call a tool which reads the file. Using the for loop, search for that NUM and result will be printed out.

awk -v Sea=a_num -v NUM=$2'
function get_f_name(file) {
e_a="NumberReceived]5"
e_b="NumberReceived]5"
cmd="/sbin/ls -rt " 'SEA'
cmd | getline FILE;close(cmd);
{if (system("/sbin/ls -rt " SEA " 2>/dev/null" ) ==0) {
tool=(system("which TOOL "))
#How would one be able to call the TOOL for eg
TOOL FILE | use the for loop
{for(e=1;e<NF;e++){
{if (($e~/'$e_a'/ && ($(e-1)~/'NUM')'/)))
{ print $0;next}
}else{
print "Chris FAILED"
}
}
}

{if ($0~/[a-z,+]:/){
SEA="*/*/*"$6"."$7"*"$8"*"
printf "%15s %15s %15s %15s %15s %s\n",$1,$2,$3,$4,$5/10,SEA
}
if(Sea="a_num"){
get_f_name()
}
else
{if (Sea="b_num"){
#call the next function
}
}
}
}' Chris_num

Many Thanks
Chris


 
Hi,

Is anyone online or is just me?

PHV|any other GURU,
Please assist.

Thanks Chris
 
Hi All,

If the above explanation not clear enough?

Many Thanks
Chris
 
Take a look at How To Ask Questions The Smart Way


Here are some quotes...
Be explicit about the question you have

Open-ended questions tend to be perceived as open-ended time sinks. The people most likely to be able to give you a useful answer are also the busiest people (if only because they take on the most work themselves). People like that are allergic to open-ended time sinks, thus they tend to be allergic to open-ended questions.
To understand the world the experts live in, think of expertise as an abundant resource and time to respond as a scarce one. The less of a time commitment you implicitly ask for, the more likely you are to get an answer from someone really good and really busy.
Never assume you are entitled to an answer. You are not; you aren't, after all, paying for the service. You will earn an answer, if you earn it, by asking a question that is substantial, interesting, and thought-provoking — one that implicitly contributes to the experience of the community rather than merely passively demanding knowledge from others.
 
Hi Igor,

Thanks for your response.

I do agree with the above statements and aslo they way I went about asking my question.

So if it is possible I will re-phrase my question.

Is it possible to use the for loop in a function, so as to search for a given Variable for e.g.

I tried the method below but I got different results

{while ( ( getline < "system(TOOL2" "FILE)" ) > 0 )
{for(e=1;e<NF;e++){
if ($e~1111111)
print $0}
close("system(TOOL2" "FILE)")
TOOL2 being the tool used to read the file (FILE)

and if there is a different way, I am eager to learn.

Regards
Chris
 
I think you are confused between system and getline. From man awk...
system(cmd) Executes cmd and returns its exit status
The built-in function getline sets $0 to the next input record from the current input file; getline < file sets $0 to the next record from file. getline x sets variable x instead. Finally, cmd | getline pipes the output of cmd into getline; each call of getline returns the next line of output from cmd. In all cases, getline returns 1 for a successful input, 0 for end of file, and -1 for an error.
I think you will need to use something like...
Code:
{
  FILE = "file1"
  TOOL = "cat" #---or whatever
  CMD = TOOL " " FILE #---build your command line
  while (CMD | getline) {
    print $0 #---or whatever
 }
 close(FILE) #--probably unnecessary
}
 
i Ygor,

Thanks Once Again.

Yes I did use the "while (CMD | getline)", but what I would like to do after the getline is to go into a for loop in order to search for a common field and see if "$e ~11111111" for e.g

The result will be
TEXT Format
===========
011111111 20040814092402 2222222 1 1140
HEX FORMAT
==========
DB-sub=1 39 [BasicInfo1]1 17 01 0227 092402 000089 000001 03 FF 02 01 02 [CallParty]2 9 01 0A 01 03 11111111F [NumberReceived]5 7 01 02 2222222


This is why I use e_a="NumberReceived]5".

If there is different way, please show me.

Thanks Again
Chris
 
Okay, as I understand it you are trying to find the line that contains "11111111" followed by "NumberReceived]5" which you already have defined...
Code:
  NUM = "11111111"
  e_a = "NumberReceived]5"
You have at least two choices:[ol][li]Loop thru each field and check for matches
Code:
  for (e = 2; e <= NF; e++) {
    if ($e ~ e_a && $(e-1) ~ NUM) {
      print $0
    }
  }
[/li][li]See if the line matches the two strings separated by any number of characters (no looping required)
Code:
  if ($0 ~ NUM ".*" e_a) {
    print $0
  }
[/li][/ol]
 
Hi Ygor,

Great to hear from you again.

Yes, we are getting there. Would the line look like the below.

{while ( ( getline < "system(TOOL2" "FILE)" ) > 0 )
if ($0 ~ NUM ".*" e_a) {
print $0
}
close("system(TOOL2" "FILE)")
}

Many Thanks
Chris
 
Maybe something like this ?
while(("TOOL2 "FILE)|getline)>0)
if($0~NUM".*"e_a) print $0

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Glad to see you back!

With reply to your answers, here is a list which I have tried


I get the following mesg by running the function below

sh: TOOL2: not found


###########################
function get_a_name(file)
###########################

{
e_a="NumberReceived]5"
e_b="NumberReceived]5"
e_e=$(e+4)
gsub("^0","",$1)
SEA="*/*/*"$6"."$7"*"$8"*"
{if (match($7,"df23")){
TOOL="/usr/bin/which rawR23A "
TOOL | getline TOOL2;close(TOOL)
}
}
{if(system("/sbin/ls -rt " SEA ) != 0) {
print ("Failed!!!");
}else{
print ("GOOD");
tool="/sbin/ls -rt " SEA
tool | getline FILE ;close(tool)


while((("TOOL2 " FILE)|getline)>0)
if($0~NUM".*"e_a) print $0

}
}
}

The reason I use which rawR23A (tools to read ) is because it could reside in any Dir and since we have a couple of machines
,each one differes.

If I replace TOOL2 with its orig path and the FILE with its real path/filename it returns an err mesg

awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The input line number is 4. The file is Chris_num.
The source line number is 30.
Exit 2


Any suggestions

Many Thanks
Chris
 
Replace this:
while((("TOOL2 " FILE)|getline)>0)
By this:
while(((TOOL2 " " FILE)|getline)>0)

And what is line # 30 in your awk program ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

I Changed as requested and there is no TOOL2 err, as for the line 30

3 awk -v NUM=b_num '
4
5 ###########################
6 function get_a_name(file)
7 ###########################
8
9 {
10 e_a="NumberReceived]5"
11 e_b="NumberReceived]5"
12 e_e=$(e+4)
13 gsub("^0","",$1)
14 SEA="*/*/*"$6"."$7"*"$8"*"
15 {if (match($7,"df23")){
16 TOOL="/usr/bin/which rawR23A "
17 TOOL | getline TOOL2;close(TOOL)
18 }
19 }
20 {if(system("/sbin/ls -rt " SEA ) != 0) {
21 print ("Failed!!!");
22 }else{
23 print ("GOOD");
24 tool="/sbin/ls -rt " SEA
25 tool | getline FILE ;close(tool)
26
27
28
29 while(((TOOL2" "FILE)|getline)>0)
30 if($0~$1".*"e_a) print $0
31 }
32 }
33 }
34
35 {
36 if ($0~/[a-z,+]:/){
37 SEA="*/*/*"$6"."$7"*"$8"*"
38 NUM1=$1
39 printf "%15s %15s %15s %15s %15s %s\n",$1,$2,$3,$4,$5/10,SEA
40 {if(NUM~/a_num/){
41 print (NUM)
42 get_a_name()
43 }else{
44 if(NUM~/b_num/){
45 print (NUM)
46 get_a_name()
47 }else{
48 if(NUM~/c_num/){
49 print (NUM)
50 #get_f_name()
51 }
52 }
53 }
54 }
55 }
56 }' Chris_num

Thanks Once Again
Chris
 
Code:
if($0 ~ ($1 ".*" e_a) ) print $0

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Vlad,

Thanks for your input.

Tried it and still the same err mesg.

awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The input line number is 4. The file is Chris_num.
The source line number is 30.
Exit 2


Many Thanks
Chris
 
if on Solaris, try nawk instead of awk.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Replace this:
if ($0~/[a-z,+]:/){
By this:
if ($0~/[a-z,\+]:/){

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Replaced and no luck. Still the same err

E10/20040814/tedo:03.df23.0001246.00404245.20040814164002
GOOD
awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The input line number is 4. The file is Chris_num.
The source line number is 30.
Exit 2


Thanks
Chris
 
replace

e_a="NumberReceived]5"

with

e_a="NumberReceived\]5"

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
HI Vlad,

And still the same err.

Thanks
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top