1834435 Members
2030 Online
110067 Solutions
New Discussion

Awk Help (functions)

 
Chris Frangandonis
Regular Advisor

Awk Help (functions)

Hi All,

To explain what I am trying to achive is firstly I read my input file, get a filename (SEA="*/*/*"$6"."$7"*"$8"*") and then search for that file.
Once the file is found, I call a tool (system function (which), in-order to read it in text (HEXtoTEXT) and this is when I get stuck.
Using a for loop function, search for that number ($1 or $2 if it is b_num) and print the result(s)

I tried the following but no luck

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



My script:

awk -v NUM=b_num '
function get_f_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 chris_s "
TOOL | getline TOOL2;close(TOOL)
}
}
{if(system("/sbin/ls -rt " SEA ) != 0) {
#{if(system("/usr/bin/test -r " SEA ) != 0) {
print ("Failed!!!");
}else{
print ("GOOD");
tool="/sbin/ls -rt " SEA
tool | getline RES ;close(tool)

while ( ( getline < "system(TOOL2" "RES)" ) > 0 ) <---- This is where I get stuck
{for(e=1;e if ($e~12345678){
getline
print $0
}}}}
close("system(TOOL2" "RES)")
}}


{if ($0~/[a-z,+]:/){
SEA="*/*/*"$6"."$7"*"$8"*"
NUM1=$1
printf "%15s %15s %15s %15s %15s %s\n",$1,$2,$3,$4,$5/10,SEA
{if(NUM~/a_num/){
print (NUM)
get_a_name()
}else{
if(NUM~/b_num/){
print (NUM)
get_b_name()
}else{
if(NUM~/c_num/){
print (NUM)
get_c_name()
}}}}}
}' Chris_num


and my input file looks like this

011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866 )


If there is a different way with awk, I am eager to learn.

Regards
Chris
5 REPLIES 5
Muthukumar_5
Honored Contributor

Re: Awk Help (functions)

Hai Chris,

system command on awk program only returns the exit status of executed command.

And more input file contents 011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866 are not being enough to look in to the script and debug for the problem.


As easy why do n't you do the shell operations in the shell script and execute on awk program simply. But your script seems to be hard to lookup. :)


If you give input file full informations and what you are expecting from the input to final output then that will make responder(S) to give their own and good idea too.

Regards
Muthu
Easy to suggest when don't know about the problem!
Chris Frangandonis
Regular Advisor

Re: Awk Help (functions)

Hi Muthu,

Thanks for your responce.

I agree with u that in a simple script it will do the trick, I know awk will do it faster.

Could I maybe explain in a different way.

=================================
function get_f_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 chris_s "
TOOL | getline TOOL2;close(TOOL)
}
}
{if(system("/sbin/ls -rt " SEA ) != 0){
print ("Failed!!!")
}else{
print ("GOOD");
tool="/sbin/ls -rt " SEA
tool | getline RES ;close(tool)

while ( ( getline < "system (TOOL2" "RES)" ) > 0 ) <---- This is where I get stuck
{for(e=1;e if ($e~12345678){
getline
print $0
}
}
}
}

close("system(TOOL2" "RES)")
}
}

I have no problem in calling a system function as it works and also finding the file.
Once the file is found, I read it with a tool (called by a system function, which works), converts it from text to hex
with no problems. My dificult part is ,while reading the file, use a for loop to search for a specific number at a specific
place by using the variables (e_a="NumberReceived]5" etc). Can this be done and how???????

Many Thanks
Chris
Chris Frangandonis
Regular Advisor

Re: Awk Help (functions)

Hi All,

To add additional info

My input file

A_NUM DATE B_NUM U DUR FILENAME
=========================================================
.011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866

and the result should look like this

011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866

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


Regards
Chris
Muthukumar_5
Honored Contributor

Re: Awk Help (functions)

hai chris

we can make the input file to output files easily but is there any relation between them ( common pattern ?!? )

I can make
A_NUM DATE B_NUM U DUR FILENAME
=========================================================
.011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866

input lines to ouput first line of

011111111 20040814092402 2222222 1 1140 tedo:04 df23 403866

as,

sed -e 's/^\.//g' filename | grep '^[0-9]'

I see no relation on the second output line to input line. And even on your awk scripts too? IF we have come patterns and transformations it is very easy to translate it out.

It will be great and more helpful with the full input files and how the transformation of second output line happening so that to make scripts to do your requirement in a crisp manner.

I am only seeing the relation of 2222222 on 2nd line from input.

And more execution rate is not much differing on awk and as well as scripts.

Regards
Muthu
Easy to suggest when don't know about the problem!
Elmar P. Kolkman
Honored Contributor

Re: Awk Help (functions)

A solution I've posted before for similar problems: concatenate the other files, like TOOL2, with markers and then the input you need. In your AWK script you make a state var that makes sure what you're reading.

Your markers could be something else if that's easier, of course.

For instance:

( echo "TOOL2" ; TOOL2 ; echo "INPUT" ; cat input ) | awk '
BEGIN { }
/^TOOL2$/ { state='TOOL2';next }
/^INPUT$/ { state='INPUT';next }
state == 'TOOL2' { }
state == 'INPUT' { }
END { }
'
Every problem has at least one solution. Only some solutions are harder to find.