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

Awk only printing lines I'm changing

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Good evening.

I am trying to modify an awk program I received tremendous help with in this awk forum. I am working with a fortran file, and I want to change the value of one array element (the tolerance) to some other value. I also want the program to print to file all other lines of the file.

Here is my code:
Code:
# match CALL VVD anywhere on a line
/CALL VVD/ {
        # split lines up by more than one commas
        split($0,a,", +")
        ind=a[1]
        pred[ind]=a[2]
        # if precision does not have "D" in it, append "D0"
        if (prec[ind] !~ "D") {
              prec[ind]=prec[ind]"D0"
        }
        oldtol=a[3]
        newtol="1D-10"
        # if the tolerance was not on this line, it
        # must be on the next
        if (oldtol == "") {
                print
                getline
                n=split($0,a,", +")
                oldtol=a[1]
                # remove line continuation and white space
                sub("[[:space:]:]*","",oldtol)
        }
        sub(oldtol,newtol)
        print
        next
}
# print any other lines in the Fortran input
FNR != NR { print }

The values for the tolerance are changed to 1D-10, but those are the only lines which are printed. Could someone help with how to get the rest of the lines to print?

Here is my input file:
SUBROUTINE T_ARG ( STATUS )
*+
* - - - - - - - - - -
* T _ A R G
* - - - - - - - - - -
*
* Test ARG subroutine.
*
* Returned:
* STATUS LOGICAL .TRUE. = success, .FALSE. = fail
*
* Called: ARG, VVD.
*
* Last revision: 7 November 2008
*
*
*-
IMPLICIT NONE
LOGICAL STATUS
DOUBLE PRECISION ANGLE(11), DAY
INTEGER IYEAR

IYEAR = 08
DAY = 311.5

CALL ARG ( IYEAR, DAY, ANGLE )
CALL VVD ( ANGLE(1), 0.5785483804494191418D0, 1D-12, 'ARG',
. 'ANGLE1', STATUS )
CALL VVD ( ANGLE(2), 6.28318080000000023D0, 1D-12, 'ARG',
. 'ANGLE2', STATUS )
CALL VVD ( ANGLE(3), 5.897950576134803669D0, 1D-12, 'ARG',
. 'ANGLE3', STATUS )
CALL VVD ( ANGLE(4), 1.615958909210931438D0, 1D-12, 'ARG',
. 'ANGLE4', STATUS )
CALL VVD ( ANGLE(5), 2.378775781400364053D0, 1D-12, 'ARG',
. 'ANGLE5', STATUS )
CALL VVD ( ANGLE(6), 4.482957906228527634D0, 1D-12, 'ARG',
. 'ANGLE6', STATUS )
CALL VVD ( ANGLE(7), 3.904405018599632626D0, 1D-12, 'ARG',
. 'ANGLE7', STATUS )
CALL VVD ( ANGLE(8), 3.519174794734325928D0, 1D-12, 'ARG',
. 'ANGLE8', STATUS )
CALL VVD ( ANGLE(9), 1.037427808761705705D0, 1D-12, 'ARG',
. 'ANGLE9', STATUS )
CALL VVD ( ANGLE(10), 0.9637917514941207742D0, 1D-12, 'ARG',
. 'ANGLE10', STATUS )
CALL VVD ( ANGLE(11), 1.615972056390525324D0, 1D-12, 'ARG',
. 'ANGLE11', STATUS )

END


Here is the output I get so far (missing most of the lines):
CALL VVD ( ANGLE(1), 0.5785483804494191418D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(2), 6.28318080000000023D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(3), 5.897950576134803669D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(4), 1.615958909210931438D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(5), 2.378775781400364053D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(6), 4.482957906228527634D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(7), 3.904405018599632626D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(8), 3.519174794734325928D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(9), 1.037427808761705705D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(10), 0.9637917514941207742D0, 1D-10, 'ARG',
CALL VVD ( ANGLE(11), 1.615972056390525324D0, 1D-10, 'ARG',

Thanks for looking.
 
Hi

starlite79 said:
FNR != NR { print }
And how are you running that [tt]awk[/tt] script ?

Because from your words I understand that you have one input file, but using a [tt]FNR != NR[/tt] condition supposes more than one input file.

Having a single input file, the line I quoted above has no effect. ( Try it : change it to [tt][blue]FNR[/blue] [teal]!=[/teal] [blue]NR[/blue] [teal]{[/teal] print [green]"I DO NOTHING"[/green] [teal]}[/teal][/tt] and you will not see the text "I DO NOTHING" in the output. ) And in your code nothing indicates the possible processing of more input files.

Sorry, I can suggest nothing to solve your problem, as I not understand you goal. Could you post the complete desired output for that input file ?

Feherke.
 
I think you just need to omit the FNR != NR to do what you require, i.e.

Code:
# print any other lines in the Fortran input
{ print }

Annihilannic.
 
Thanks Feherke and Annihilannic! Feherke, my command line said "awk -f tolerance input_f > output_f", where tolerance was the name of the awk program.
 
because of the /CALL VVD/ condition before your program block, that block is only executed for the FORTRAN lines containing that search string...

you can go like this
Code:
/CALL VVD/{ ...(your VVD code block) }
! /CALL VVD/{print}

But I'm not sure I like this syntax... An alternative:
Code:
{
 if ($0 ~ /CALL VVD/) {
  ...(your VVD code block)
 }
 else {
  print
 }
}



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top