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!

Function call 3

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi PHV,Futurelet or All

How can I call a function from the example given below.
T_S is the unix timestamp (eg 073810000 ,1073810002..).

What I would like to do is whenever the variable T_S is assigned, I would pass it to the function, which inturn convert it to date format (function Time_S )and then printf out.

Time_S(){
echo "0d$1=Y"|adb |sed -e 's/://g'| awk '
BEGIN{OFS=""}
{$2=(index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)-1)/3+1
if($2<10)$2="0"$2
if($3<10)$3="0"$3
print
}'
}

cat FILE | awk '{for (e=1;e<=NF;e++){if (match($e,"CHRIS")){if($82!="V"){st=$85}{if($154=="M"){T_S=$155}{printf "%s %15.f",st,T_S}}}}'

Many Thanks
Chris
 
The result of the Time_S funvtion is a string, modify your print statement :

[tt]printf "%s\n",Time_S($155)[/tt]

Jean Pierre.
 
JP, Time_S returns a string of 14 digits, so it should be converted to a float.
Chris, below the version I posted and that works for me:
function Time_S(ut ,cmd,buf,a){
cmd="echo '0d"ut"=Y'|adb|sed -n 's!^[ \t]*!!;s!:!!gp'"
cmd|getline buf;close(cmd);split(buf,a," ")
a[2]=(index("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])+2)/3
return sprintf("%04d%02d%02d%06d",a[1],a[2],a[3],a[4])
}
Pay attention to the sed stuff

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

With the sed -n 's!^[ \t]*!!;s!:!!gp'" I get the following error message and if I run with sed -e 's/://g' then no error message but wrong results


if ($0~/^<AT/) {
getline; next;
getline; next;
getline; next;
}
}
/^S7 A/ {
printf "\n%s %s",sep,$0;sep="\n";
next
}
{
printf " %s ",$0
}
+ s!:!!gp"
cmd|getline buf;
close(cmd);
split(buf,a," ")
a[2]=(index("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])+2)/3
return sprintf("%04d%02d%02d%06d",a[1],a[2],a[3],a[4])
}
{printf "%.f\n",Time_S($155)}
./.Chris_MM4[29]: s!:!!gp"^Jcmd|getline buf;^Jclose(cmd);^Jsplit(buf,a," ")^Ja[2]=(index("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])+2)/3^Jreturn sprintf("%04d%02d%02d%06d",a[1],a[2],a[3],a[4])^J}^J{printf "%.f\n",Time_S($155)}: not found
+ cut -b1-490,500-550,680-800
Exit 1

Thanks
Chris
 
Verify quoting in the cmd assignment.

Restons Zen...

Jean Pierre.
 
Hi PHV,

Sorry, the error is

+ cut -b1-490,500-550,680-800
+ awk
function Time_S(ut ,cmd,buf,a){
cmd="echo 0dut=Y|adb|sed -n s!^[ t]*!!
syntax error The source line is 3. The function is Time_S.
The error context is
cmd="echo 0dut=Y|adb|sed -n >>> s!^[ <<<
awk: The statement cannot be correctly parsed.
The source line is 3. The function is Time_S.
awk: There is a missing } character.
+ s!:!!gp"
cmd|getline buf;
close(cmd);
split(buf,a," ")
a[2]=(index("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])+2)/3
return sprintf("%04d%02d%02d%06d",a[1],a[2],a[3],a[4])
}
{printf "%.f\n",Time_S($155)}
./.Chris_MM4[28]: s!:!!gp"^Jcmd|getline buf;^Jclose(cmd);^Jsplit(buf,a," ")^Ja[2]=(index("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])+2)/3^Jreturn sprintf("%04d%02d%02d%06d",a[1],a[2],a[3],a[4])^J}^J{printf "%.f\n",Time_S($155)}: not found
Exit 1

Thanks
Chris
 
Sorry, I didn't realise your awk program was written on the fly in the shell script inside single quote.[blush]
Try to replace this:
cmd="echo '0d"ut"=Y'|adb|sed -n 's!^[ \t]*!!;s!:!!gp'"
by this:
cmd="echo \"0d"ut"=Y\"|adb|sed -n \"s!^[ \t]*!!;s!:!!gp\""

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Many Thanks to you all.
This site is worth its weight in GOLD.

Last and final question
1)An explanation on sed -n 's!^[ \t]*!!;s!:!!gp' and how/when can I use this.
2)Where can I get info on awk function as well as examples.

PHV,
When you mentioned that I should write this in a single awk program (very curious), if so could you write out one on you spare time, so that I can learn from it and see how it should be written.

A million Thanks
Chris
 
1)
sed -n
The -n option suppress the default output
's!^[ \t]*!!
Remove all leading spaces or tabs
;
Editing command separator
s!:!!gp'
Get rid of all colons and print the pattern space if a replacement was made
Anyway, man sed
2)
man awk
Chris, post your final working version and i'll be glad to comment it.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top