Computing.Net > Forums > Unix > awk script

awk script

Reply to Message Icon

Original Message
Name: scottr99
Date: August 8, 2005 at 14:33:46 Pacific
Subject: awk script
OS: unix
CPU/Ram: 2gig
Comment:

I would like to take the following file:

!
! *** UNIGRAPHICS-TO-ANSYS CORRESPONDENCE TABLE FILE ***

! *** This version of the connection was validated with UG NX 2.0.0.21
!
! Version : 8.1 - UG NX V2.0
! Build date : 03/25/04
! Generated on: 08/08/05 15:20
! By : bz0tkb
!
! Part : junk
! Units : Inches
! Selected Layers : 1-256
! Geometry Type : Solids Only
!
! Translator Working Tolerance: 5.0000000E-05
! Part system Tolerance (ref): 1.0000000E-05
!
!Object Name ANSYS Type ANSYS ID
EDGE2 LINE 3
EDGE1 LINE 4
SIDE1 AREA 2
BOTTOM AREA 3
SIDE2 AREA 4
TOP AREA 5
SCOTT AREA

and put in a different file the following:

LSEL,S,,,3
CM,EDGE2,LINE

etc., to the end of the file.

Can somone help?


Report Offensive Message For Removal


Response Number 1
Name: scottr99
Date: August 8, 2005 at 14:52:28 Pacific
Reply: (edit)

Btw, thank-you very much for any help!! :)

Scott


Report Offensive Follow Up For Removal

Response Number 2
Name: scottr99
Date: August 8, 2005 at 15:20:16 Pacific
Reply: (edit)

Okay, the following gives me the first three lines...but I need to go to the end of file...


Like:

until EOF do
getline
printf("%s etc..
end

can someone help with the proper control flow syntax to get to the eof please...

#! /bin/nawk -f
#
# flexeng_edit.awk - Save the mount load data
# from a flexeng output listing.
#
BEGIN {

# Initialize some handy constants
TRUE = 1
FALSE = 0
fatal_error = FALSE

# Check the number of command line parameters
if ( ARGC != 2 )
{
printf("Usage: ansys_comp.awk tbl_file.tbl > ansys_component_file\n")
fatal_error = TRUE
exit(-1)
}

}

#


/^ *!Object/ {

getline
printf("%s\n",$0)
getline
printf("%s\n",$0)
getline
printf("%s\n",$0)
next
}

END {

# Don't do anything if fatal error found
if ( fatal_error == TRUE )
{
exit(-1)
}

}


Scott


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: August 9, 2005 at 06:03:04 Pacific
Reply: (edit)

Scott,

The sample input file is good, but you completely lost me on this:

and put in a different file the following:

LSEL,S,,,3
CM,EDGE2,LINE

etc., to the end of the file.

I do not see where that data comes from.


Report Offensive Follow Up For Removal

Response Number 4
Name: Luke Chi
Date: August 9, 2005 at 09:01:57 Pacific
Reply: (edit)

I'm confused from the very beginning too.

Luke Chi


Report Offensive Follow Up For Removal

Response Number 5
Name: scottr99
Date: August 9, 2005 at 13:05:56 Pacific
Reply: (edit)

Hi guys, thanks for responding! :)

Okay I have the following working...

#! /bin/nawk -f
#
# flexeng_edit.awk - Save the mount load data
# from a flexeng output listing.
#
BEGIN {

# Initialize some handy constants
TRUE = 1
FALSE = 0
fatal_error = FALSE

# Check the number of command line parameters
if ( ARGC != 2 )
{
printf("Usage: ansys_comp.awk tbl_file.tbl > ansys_component_file\n")
fatal_error = TRUE
exit(-1)
}

}

/^ *!Object/ {

getline
if ( $2 == "LINE" )
{ printf("LSEL,S,,,%s\n",$3)
printf("CM,%s,LINE\n",$1) }
else if ( $2 == "AREA" )
{ printf("ASEL,S,,,%s\n",$3)
printf("CM,%s,AREA\n",$1) }

getline
if ( $2 == "LINE" )
{ printf("LSEL,S,,,%s\n",$3)
printf("CM,%s,LINE\n",$1) }
else if ( $2 == "AREA" )
{ printf("ASEL,S,,,%s\n",$3)
printf("CM,%s,AREA\n",$1) }

getline
if ( $2 == "LINE" )
{ printf("LSEL,S,,,%s\n",$3)
printf("CM,%s,LINE\n",$1) }
else if ( $2 == "AREA" )
{ printf("ASEL,S,,,%s\n",$3)
printf("CM,%s,AREA\n",$1) }

next
}

END {

# Don't do anything if fatal error found
if ( fatal_error == TRUE )
{
exit(-1)
}

}


But, I need the chunk that is repeated 3 (or 'n') times to be put into a while loop so it just continues to the end of the file that this info is coming from.

So something like this...

while ( != EOF ) # while not end of file...
do

getline
if ( $2 == "LINE" )
{ printf("LSEL,S,,,%s\n",$3)
printf("CM,%s,LINE\n",$1) }
else if ( $2 == "AREA" )
{ printf("ASEL,S,,,%s\n",$3)
printf("CM,%s,AREA\n",$1) }

done

What do you guys think? It seems like it should be easy but I can't get it.

Thanks in advance!! :)

Scott


Report Offensive Follow Up For Removal


Response Number 6
Name: scottr99
Date: August 9, 2005 at 13:08:47 Pacific
Reply: (edit)

Okay, here's where the info is coming from:

!
! *** UNIGRAPHICS-TO-ANSYS CORRESPONDENCE TABLE FILE ***

! *** This version of the connection was validated with UG NX 2.0.0.21
!
! Version : 8.1 - UG NX V2.0
! Build date : 03/25/04
! Generated on: 08/09/05 10:34
! By : bz0tkb
!
! Part : scott1
! Units : Inches
! Selected Layers : 1-256
! Geometry Type : Solids Only
!
! Translator Working Tolerance: 5.0000000E-05
! Part system Tolerance (ref): 1.0000000E-05
!
!Object Name ANSYS Type ANSYS ID
EDGE1 LINE 4
SMEDGE LINE 8
SIDE1 AREA 1
TOP AREA 4


Report Offensive Follow Up For Removal

Response Number 7
Name: Jim Boothe
Date: August 10, 2005 at 10:45:57 Pacific
Reply: (edit)

Right, once you pop into the /!Object/ code block, you have to remain in there to process all following lines.  If you exit that code block, the main loop will bypass the remaining lines looking for the next /!Object/.

You can code the loop as follows:

/^ *!Object/ {

while (getline > 0)
if ( $2 == "LINE" )
   { printf("LSEL,S,,,%s\n",$3)
     printf("CM,%s,LINE\n",$1) }
else
if ( $2 == "AREA" )
   { printf("ASEL,S,,,%s\n",$3)
     printf("CM,%s,AREA\n",$1) }

}


And here is a different approach.  This code produces the identical output as the code above.  The BEGIN block bypasses the first part of the file through the !Object line.  With those lines out of the way, the main detail block does not need a loop.

#! /bin/sh

awk 'BEGIN \
{while (true)
   {getline
    if (match($0,"!Object"))
       break
   }
}

{if ($2 == "LINE")
   {printf("LSEL,S,,,%s\n",$3)
    printf("CM,%s,LINE\n",$1)}
else
if ($2 == "AREA")
   {printf("ASEL,S,,,%s\n",$3)
    printf("CM,%s,AREA\n",$1)}

}' filein > fileout


Report Offensive Follow Up For Removal

Response Number 8
Name: scottr99
Date: August 10, 2005 at 12:55:07 Pacific
Reply: (edit)

Jim, you're awesome. Thank-you very much! :)

Scott


Report Offensive Follow Up For Removal

Response Number 9
Name: scottr99
Date: August 18, 2005 at 06:55:02 Pacific
Reply: (edit)

I'm afraid my problem needs a bit more tweaking. That's because it needs to accomodate multiple occurances of entities of the same name. If you can possibly help, please take a look at the following description. I humbly ask for your help and would be forever grateful, honestly.

This is what I'm starting with:

!
! *** UNIGRAPHICS-TO-ANSYS CORRESPONDENCE TABLE FILE ***

! *** This version of the connection was validated with UG NX 2.0.0.21
!
! Version : 8.1 - UG NX V2.0
! Build date : 03/25/04
! Generated on: 08/18/05 08:34
! By : bz0tkb
!
! Part : block
! Units : Inches
! Selected Layers : 1-256
! Geometry Type : Solids Only
!
! Translator Working Tolerance: 5.0000000E-05
! Part system Tolerance (ref): 1.0000000E-05
!
!Object Name ANSYS Type ANSYS ID
EDGE LINE 2
FACE AREA 4
EDGE LINE 3
EDGE LINE 6
FACE AREA 5
EDGE LINE 10
FACE AREA 3


First I need this sorted to look like this (this part I don't yet have in my awk script):


EDGE LINE 2
EDGE LINE 10
EDGE LINE 3
EDGE LINE 6
FACE AREA 5
FACE AREA 4
FACE AREA 3


Then formated to look like this (I have most of this from Jim's input which I have added below but it's currently not smart enough to determine when to use the 'S' and when to use the 'A'):

LSEL,S,,,2
CM,EDGE,LINE

LSEL,A,,,10
CM,EDGE,LINE

LSEL,A,,,3
CM,EDGE,LINE

LSEL,A,,,6
CM,EDGE,LINE

ASEL,S,,,5
CM,FACE,AREA

ASEL,A,,,4
CM,FACE,AREA

ASEL,A,,,3
CM,FACE,AREA


Note that the first instance of a name must have the 'S' and following instances of that same name must have the 'A'.

This is what I currently have from Jim which works perfect but only for a single occurance of some entity name:

while ( getline > 0 )
if ( $2 == "LINE" )
{ printf("LSEL,S,,,%s\n",$3)
printf("CM,%s,LINE\n",$1) }
else if ( $2 == "AREA" )
{ printf("ASEL,S,,,%s\n",$3)
printf("CM,%s,AREA\n",$1) }


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: awk script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge