Understanding the awk Command

Asked 1 years ago, Updated 1 years ago, 45 views

I would like to double the first three 222 of 22 (this value fluctuates) (calculation result 44441) in a file with the following contents written on it.How should I express it?

&electrons
/
K_POINTS {automatic}
2 2 2 1 1 1
CELL_PARAMETERS {angstrom}
4.255648834196731  -2.457000000000000   0.000000000000000 
0.000000000000000   4.914000000000000   0.000000000000000 
0.000000000000000   0.000000000000000   5.406000000000000 
ATOMIC_SPECIES

コメント Reflect the contents of the comments

The following are the criteria for identifying the data:

before change

K_POINTS {automatic}
a a a b b b b b b b b b b b b b b b b b

modified

K_POINTS {automatic}
2 * a 2 * a 2 * ab b b

The above file is an input file used for Self-Consistent Field (SCF) calculations, and the K_POINTS format is Input File Description:K_POINTS automatic.

awk

2022-09-29 22:08

1 Answers

Example script.
Once you have found a line containing K_POINTS, read the following line and double the first three numbers to output:

awk'{
    if(match($0,/K_POINTS/)!=0) {#K_POINTS containing rows
        print$0
        getline# For the following lines:
        num = split($0,a)
        dlmt=""
        for (no=1; no<=num;no++) {
            if(no<=3){#For the first three lines
                printf("%s%d", dlmt, a[no]*2)# Double the value
            }
            else{
                printf("%s%d", dlmt, a[no])
            }
            dlmt=""# delimiter blank
        }
        printf("\n")
    }
    else{
        print$0
    }
}
'


2022-09-29 22:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.