change a field in a file

Hi, All
I want to change a field in the middle of a file such as below

123 456 789
987 654 321
147 258 369

for example i extract 654 in line 2 with following code and sum with 20

EX=`cut -f2 -d " " FILENAME`
let EX=$EX+2

if we have too many fields with too many lines, how we can extract
(n,m) [n=line number , m= field number] of this file and replace it with
an other number, with out rewrite all file?

What are the constants of the file?

Will it always be line 2, field 2?

Your question is not quite clear but try if this is what you wanted...
654 will be the number which u will be searchin in each field one by one and it will add 2 when it find the match for 654

 
awk '{for(i=1;i<=NF;i++){if($i==654)$i=$i + 2; print $i}}' infile | paste - - -

So Advanced, Thank you

It was powefull, and can any body tell me how can i save only this cell , without need to overwrite all file?
Assume that, there is too many records (a huge file).

Shorter :rolleyes:

awk '{for(i=0;++i<NF;){if($i==654)$i+=2}}1' file

when i run your code, it changes only on the second row on the second column. how about on the last one, there is 654???

>awk '{for(i=0;++i<NF;){if($i==654)$i+=2}}1' file
123 456 789
987 656 321
147 258 369
222 333 654
i<NF

should be:

i<=NF

:slight_smile:

replace

++i<NF

by ++i<=NF

One missing char and the result is different.

awk '{for(i=0;++i<=NF;){if($i==654)$i+=2}}1' file

My bad :wink:

ok.
Why it can't make changes inside the file?:frowning:

awk script doesnt modify the actual file, you have to save it to another file and rename it to your original file.

you just want to change every 654 to 656?

oh,my god....

sed 's/\b654\b/656/g' yourOldfile > newfile