Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Sed and Awk (http://www.programmingforums.org/forum22.html)
-   -   Awk In Awk... (http://www.programmingforums.org/showthread.php?t=504)

keenlearner Sep 10th, 2004 11:17 AM

hi friends,

how to use one awk within another awk.....
i want to o actually...something like....

awk ' { if ( $1 == 1 )

awk ' { var=$1 }' 2ndfile

}' 1stfile

but this 2nd awk is not working.....
it gives error......
my problem is ...
that if the first field of 1stfile is 1 then only store the value of 2ndfile
in var otherwise not...

but i amnot able to get the ouput......

HELP ME OUT FRIENDS........... :( :( :(

erebus Sep 10th, 2004 11:10 PM

Quote:

Originally posted by keenlearner@Sep 10 2004, 04:17 PM
awk ' { if ( $1 == 1 )

awk ' { var=$1 }' 2ndfile

}' 1stfile

For something like that code in perticular to work, you would have to use the system() function or getline to run awk within an awk process, unless you just want to pass the output of one to another, then you would just run it through a pipe. From what you said you wanted to do, I don't think this is necessary at all. Using a program recursively like this is usually wasted cycles since you are forking extra processes.

I know doing the above is bad practice, though I can't think of anything to accomplish that. One thing I also noticed about your code is that you want to store a variable of one awk program as if it were from the mother awk that forked it, which wouldn't work. Also, this program is working in 'main' loop mode, which goes in the form while(<>), process line, which goes through every line. In this case, that would mean you're replacing 'var' every single time, which doesn't seem to be the desired result, though I may be wrong.

I have actually been trying to work out this problem since I got home from school, and will be waiting for a better explanation and logic of what you want to do so I can help better. Hope to here from you soon...

keenlearner Sep 12th, 2004 9:54 AM

Thanx for ur reply....

ok ...i will explain my problem that what exactly i want....

i am basically writing a program for backup...of a particular directory , if it's size increased by 1024 bytes everytime.

i have two files with me
1. flag
2. size

initially flag has a value 0
prg will first read this file and if the value is 0 then it will take size of /dir through du command and store it in the file size.
and also the value of flag file will change to 1.

now the second time again flag file wll be readed and as now the value is changed to 1 so here the need arise to have the value from the size file and store it in a variable and then through some calculations compare it and judge that if the resultant value is greater than or equal to 1024 bytes than again du coomand should run and take the backup in a new file...

so for thsi scenario i need awk command and off course need to store the value...

so my code is something like...
du -sb /home > size
awk '{ if ( $1 == 0 )
{
du -sb /home > newsize
TAKE BACKUP
echo 1 > flag
}
else
{
awk '{ var1=$1 }' size
awk '{ var2=$1 }' newsize
if ( (var1-var2) >= 1024 )
{
TAKE BACKUP
du -sb > newsize
}
}
}' flag

my code is not exactly same but something of this sort....
but here else part is not at all working...
the problem i am facing is that awk with in another awk is not running ,
niether i am able to store the output of awk in any variable which i can use further.....

i hope u understand my problem well....

pls guide me in a right direction.....

TAKE CARE

erebus Sep 13th, 2004 3:31 AM

Not at a *nix terminal at the moment, but i'll work on it when I get home. Might want to reiterate the logic of the program, not code, k:
:

#!/bin/sh
du -s | awk '{ print $1 }' > /home/size
var = `cat /home/size`

if [var = 0]
then
  du -sb /home > newsize
  echo 1 > flag
else
  var2 = `awk '{ print $1 }'size`
  var3 = `awk '{ print $1 }'newsize`
  var4 = `echo $var2 - $var3 | bc`
 
  if [ var4 >= 1024 ]
  then
        du -sb /home > newsize
  fi
fi


keenlearner Sep 14th, 2004 2:11 PM

Thanx Buddy!

ur code really works..

Thank you very much for ur kind help.....

ALL THE BEST
TAKE CARE
Regards


All times are GMT -5. The time now is 5:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC