Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 10th, 2004, 11:17 AM   #1
keenlearner
Newbie
 
Join Date: Sep 2004
Posts: 13
Rep Power: 0 keenlearner is on a distinguished road
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...........
keenlearner is offline   Reply With Quote
Old Sep 10th, 2004, 11:10 PM   #2
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
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...
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Sep 12th, 2004, 9:54 AM   #3
keenlearner
Newbie
 
Join Date: Sep 2004
Posts: 13
Rep Power: 0 keenlearner is on a distinguished road
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
keenlearner is offline   Reply With Quote
Old Sep 13th, 2004, 3:31 AM   #4
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
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
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Sep 14th, 2004, 2:11 PM   #5
keenlearner
Newbie
 
Join Date: Sep 2004
Posts: 13
Rep Power: 0 keenlearner is on a distinguished road
Thanx Buddy!

ur code really works..

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

ALL THE BEST
TAKE CARE
Regards
keenlearner is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:15 PM.

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