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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help again 3

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
Hello Friends
Thanks for those who have replied to my earlier post.
Again I am here with a basic doubt.
I have a text format here

Increment 1
-----------
Max value 100
Min Value 200

NODE U1 U2 U3


59 -1.4877E-03 0.000 -0.3494
203 -7.3659E-03 0.000 -1.116

Increment 2
-----------
Max value 150
Min Value 250

NODE U1 U2 U3


59 -1.4877E-03 0.000 -0.3494
203 -7.3659E-03 0.000 -1.116


and so on.. I need to get the values of Maximum,Minimum,U2 (2 values for each node),U3 (2 values for each node) for each increment in to a separate text file.
Please help out.

Regards
MP
 
Try this.
Code:
{
  if ($1 == "Increment") {
    if (ix) {
      print f3min " " f3max > fn
      print f4min " " f4max > fn
      close(fn)
    }
    fn = "file" ++ix
    flg = 0
  }
  else if ($1 == "NODE") {
    flg = 1
  }
  else if (flg && NF>0) {
    if (flg == 1 || $3 > f3max) f3max = $3
    if (flg == 1 || $3 < f3min) f3min = $3
    if (flg == 1 || $4 > f4max) f4max = $4
    if (flg == 1 || $4 < f4min) f4min = $4
    flg++
  }
}
END {
      print f3min &quot; &quot; f3max > fn
      print f4min &quot; &quot; f4max > fn
Hope this helps. CaKiwi
 
Hello CaKiwi
When I run the above program and included a #! /bin/awk -f in the first line and compiled like this.
shellprompt:awkscript test.dat
where test.dat is the one to be parsed and awkscript is above code. the error is

awk: A print or getline function must have a file name.
The input line number is 856. The file is test.dat.
The source line number is 26.

Help out.

 
AWK isn't like shell scripting languages. You need to ALWAYS specify
Code:
$ awk -f awkscript test.dat
on the command-line. You can create a shell script out of this line, but the awk script itself cannot be made executable.

Sorry

Einstein47
(How come we never see the headline, &quot;Psychic Wins Lottery&quot;?)
 
not really. The 'myScript.awk myDataFile.txt' construct is a valid calling sequence.

'myScript.awk' specifies a valid awk interpreter [as any other interpretive script] on the first line and it's being made executable [chmod 555 myScript.awk]:

#!/usr/bin/nawk -f

vlad
 
Sorry, Einstein47, you are mistaken. These days you can generally create a shell script for awk, depending on your OS. One problem I see is that I missed the final right brace } when I cut and pasted my solution, but I don't think that is the problem. It looks like the script is never finding the word Increment in the file. Are there 856 lines in your data file? Add the following to the END section and tell me what output you get.
Code:
END{ 
   if (ix==0) print &quot;Increment not found&quot;
   print f3min &quot; &quot; f3max > fn
   print f4min &quot; &quot; f4max > fn
   }
CaKiwi
 
WOW!!

I am mistaken - and I have learned something new as well. Thanks guys -
star.gif
s for both of you. Einstein47
(How come we never see the headline, &quot;Psychic Wins Lottery&quot;?)
 
Hello Everybody
Thanks for the responses and I apologise for posting my question wrongly. Again here is my sample one of the increment data looks like. I find it very very difficult in explaining the question. Pls pardon me on my skills.

INCREMENT 1 SUMMARY


TIME INCREMENT COMPLETED 0.100
STEP TIME COMPLETED 0.100

E L E M E N T O U T P U T


THE FOLLOWING TABLE IS PRINTED FOR ELEMENT

ELEMENT PT SF1 SF2


20721 1 -5.828 157.9
20722 1 9.452 -135.4
20723 1 -4.727 -109.0
20724 1 8.991 85.39
20725 1 3.992 -23.74



TOTAL 11.88 -24.96

ELEMENT PT U1 U2


20721 1 -1.828 15.9
20722 1 0.452 -13.65
20723 1 -5.727 -19.34
20724 1 0.991 65.39
20725 1 56.9 3.74



TOTAL 50.84 -58.96

There are 2 tables of data in the above example. 1 is SF1 data and other one is U1 data. Each has its own total values.

I need to get the TOTAL values only for SF1 table(i.e in this sample data - 11.88 and -24.96).
If I give $1==&quot;TOTAL&quot; and print $2 and $3. It will print the whole lot i.e the total values for both tables

I need the total values for only SF1 and SF2 and not for U1 and U2 table.
How do I parse this. The above sample data is only for increment 1 and there are lots of increments that I need to get the total values.
 
It is going to be difficult to help you unless you can define your requirements more precisely. But maybe this is close to what you want.
Code:
{
  if ($1 == &quot;ELEMENT&quot; && $3 == &quot;SF1&quot;) flg = 1
  if (flg && $1 == &quot;TOTAL&quot;) {
    print $2 &quot; &quot; $3
    flg = 0
  }
}
CaKiwi
 
Hello CaKiwi
Thanks for the answer. I am looking similar and found the solution based upon ur answers. Thx a lot. But again got in to a problem.I cut and pasted the above code what you have given and ran the script
$ myscript test.dat
But there is nothing on the screen and it returns one more shell prompt like this

$ myscript test.dat
$

Where I am wrong in running the script.

Regads
Murugs
 
I think myscript test.dat should work. Pls refer to the messages above. But there is some other problem which I am not able to figure it out. Syntax and everything is correct.whats happening...
 
Jeez.
Please do this.

insert the first line of cakiwi's excellent scripts
with #!/bin/sh
Then try sh -vx scriptname to debug it visibly.
You'll see where it isn't going this way.
 
Hello marsd
Thanks and it worked and popped an error like this
when I ran sh -vx myscript test.dat

#! /bin/sh

{
if ($1 == &quot;ELEMENT&quot; && $3 == &quot;SF1&quot;) flg script2[3]: Syntax error at line 4 : `flg' is not expected.

Regards
Murugs
 
marsd,

CaKiwi's script was in awk. I assumed the OP copy/pasted it 'as is'.

 
Right.

Try this with the earlier suggestion.

#!/bin/sh

awk '
{
if ($1 == &quot;ELEMENT&quot; && $3 == &quot;SF1&quot;) flg = 1
if (flg && $1 == &quot;TOTAL&quot;) {
print $2 &quot; &quot; $3
flg = 0
}
}' $1
 
Hello marsd
Below is the output when I performed sh -vx script test.dat

#!/bin/sh

awk '
{
if ($1 == &quot;ELEMENT&quot; && $3 == &quot;SF1&quot;) flg = 1
if (flg && $1 == &quot;TOTAL&quot;) {
print $2 &quot; &quot; $3
flg = 0
}
}' $1
+ awk
{
if ($1 == &quot;ELEMENT&quot; && $3 == &quot;SF1&quot;) flg = 1
if (flg && $1 == &quot;TOTAL&quot;) {
print $2 &quot; &quot; $3
flg = 0
}
} test.dat


The program is available but the ouput is not..Please suggest some other method or ways or If you are interested I can mail u the data file so that you can test it at your end.

regards
Murugs
 
Put some print statements in the script to help with debugging. I suggest the following to start with:
Code:
{
  print &quot;-> &quot; $0
  if ($1 == &quot;ELEMENT&quot; && $3 == &quot;SF1&quot;) flg = 1
  print &quot;-> &quot; $1 &quot; &quot; $3 &quot; flg = &quot; flg
  if (flg && $1 == &quot;TOTAL&quot;) {
    print $2 &quot; &quot; $3
    flg = 0
  }
BTW, does your file have tabs in it? CaKiwi
 
Hello All
I have included some print statements and nothing is working...
BTW the output of above program is again the same..

$myscript test.dat
myscript5[4]: Syntax error at line 4 : `flg' is not expected.

My test.dat file has some tabs and fields arranged neatly

Regards
Murugs
 
Take out the line
#! /bin/awk -f
(if any) out of the script and run it by entering
awk -f myscript test.dat CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top