Replacing certain fields from certain rows

Hi all,

say for example i have the next input file

   30

Au      7.500000    7.500000    5.774000
Au      7.500000    8.995000    8.363000
Au      7.500000    6.005000    8.363000
Au     20.633000    7.500000    9.226000
Au     20.632000    6.005000    6.637000
Au     20.632000    8.995000    6.637000
Au      9.755453    7.933470    7.400875
Au     18.407230    7.093490    7.665465
H      11.819851    5.334439   10.410752
C      11.940178    5.715861    9.377912
C      13.168054    6.318866    9.010767
S      14.466122    6.254196   10.233180
C      13.396476    6.923076    7.720407
C      12.297938    6.830318    6.824263
H      12.401503    7.219851    5.792611
C      11.048036    6.178032    7.146598
H      10.441122    5.788852    6.299594
C      10.851494    5.591076    8.472222
S       9.412998    4.693344    9.008371
S      15.240980    8.199293    9.985104
C      15.636569    8.091816    8.246799
C      16.885748    8.701619    7.834007
H      17.409941    9.354856    8.565981
C      17.223130    8.827686    6.413005
S      18.529427    9.874926    5.768856
C      16.296723    8.218102    5.498867
H      16.532233    8.233681    4.415469
C      15.107140    7.612621    5.934757
H      14.460039    7.154082    5.160631
C      14.709295    7.536656    7.313049

and i just want to add 0.1 to the second field of the 6th 7th and 8th row; so the final output would be:

   30

Au      7.500000    7.500000    5.774000
Au      7.500000    8.995000    8.363000
Au      7.500000    6.005000    8.363000
Au     20.733000    7.500000    9.226000
Au     20.732000    6.005000    6.637000
Au     20.732000    8.995000    6.637000
Au      9.755453    7.933470    7.400875
Au     18.407230    7.093490    7.665465
H      11.819851    5.334439   10.410752
C      11.940178    5.715861    9.377912
C      13.168054    6.318866    9.010767
S      14.466122    6.254196   10.233180
C      13.396476    6.923076    7.720407
C      12.297938    6.830318    6.824263
H      12.401503    7.219851    5.792611
C      11.048036    6.178032    7.146598
H      10.441122    5.788852    6.299594
C      10.851494    5.591076    8.472222
S       9.412998    4.693344    9.008371
S      15.240980    8.199293    9.985104
C      15.636569    8.091816    8.246799
C      16.885748    8.701619    7.834007
H      17.409941    9.354856    8.565981
C      17.223130    8.827686    6.413005
S      18.529427    9.874926    5.768856
C      16.296723    8.218102    5.498867
H      16.532233    8.233681    4.415469
C      15.107140    7.612621    5.934757
H      14.460039    7.154082    5.160631
C      14.709295    7.536656    7.313049

How can i manage to do that with awk?
Thanks

Try:

awk 'NR>5&&NR<9{$2+=0.1}1' file

That really worked just fine! Thank you!

@Bartus:
I have a question about this solution just to understand better:
What is the significance of 1 (red) in the end ?
awk 'NR>5&&NR<9{$2+=0.1}1' file And what would be the Perl equivalent for this solution?
Cheers :slight_smile:

---------- Post updated at 07:43 AM ---------- Previous update was at 07:43 AM ----------

@Bartus:
I have a question about this solution just to understand better:
What is the significance of 1 (red) in the end ?

awk 'NR>5&&NR<9{$2+=0.1}1' file

And what would be the Perl equivalent for this solution?
Cheers :slight_smile:

The "1" at the end of AWK command is the same as putting "print" statement there. Perl version of that code:

perl -anle '$F[1]+=0.1 if $.>5 && $.<9;print join " ",@F' file

Output is a bit messed up now, but it can be easily adjusted using printf formatting.