Paulo Barros
Paulo Barros

Reputation: 157

Alter values in two differrent lines with awk

I have a parameter card that's fixed. I need to change the value of lines 10 and 20, I am trying using awk but so far didn't get it. Those positions are fixed, so I will need to do this with a loop but it will be always the same 2 lines, lines 10 and 20. I tried this way but it changes the $1 of all lines from 10 to 20. Since I will be changing these values I cannot use sed to match a pattern because I won't have one, the values will be random each time, only the lines are fixed.

awk 'FNR==10,FNR==20{$1="new_value"}1' dummycard.txt > dummynew.txt  

Thatś my file:

DATAFILE
p365.txt
TRAITS
2
FIELDS_PASSED TO OUTPUT

WEIGHT(S)

RESIDUAL_VARIANCE
300
EFFECT
3 cross numer
EFFECT
1 cross numer
RANDOM
animal
FILE
pednem.txt
(CO)VARIANCES
200
OPTION conv_crit 1d-12
Convergence criterion (default 1d-10).
OPTION maxrounds 10000
maximum rounds (default 5000).

Upvotes: 1

Views: 41

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133650

You should give OR condition for FNR checking.

awk 'FNR==10 || FNR==20{$1="new_value"}1' Input_file

OR as per Ed sir's suggestion, you could try:

awk 'FNR ~ /^[12]0$/{$1="new_value"}1' Input_file


In case you want to have your new value in an awk variable then try following.

awk -v new_value="10" 'FNR==10 || FNR==20{$1=new_value}1' Input_file

For example I have set its value to 10 you could set it as per your need.

Why OP's code didn't work: Since OP is using , between line numbers so it is taken as a range thus all lines changed from line numbers 10 to 20.

Upvotes: 2

Related Questions