0

I find the string I want to replace this way:

awk '/%block lattice_cart/{flag=1;next}/%endblock lattice_cart/{flag=0}flag {print $1}' pacet.cell | awk 'NR==1' 

How can I replace the value found, for instance: "5 or any number in entry (1,1)" with "3"

Input:

%block lattice_cart
any_number   any_number  any_number
any_number   any_number  any_number
any_number   any_number  any_number
%endblock lattice_cart

Expected output:

%block lattice_cart
3.00000   any_number  any_number
any_number   any_number  any_number
any_number   any_number  any_number
%endblock lattice_cart
Inian
  • 80,270
  • 14
  • 142
  • 161
Caterina
  • 775
  • 9
  • 26
  • Post some sample text and show how you like to output to be. It looks like you get data between start/stop pattern, and the just select one line `'NR==1'`, strange. – Jotne Jul 23 '19 at 11:45
  • Yes I want to choose the individual entries of the "matrix" or grid – Caterina Jul 23 '19 at 11:48
  • The example is just for the first one, but I have to modify all the diagonal entries for some other value – Caterina Jul 23 '19 at 11:49
  • This may help you https://stackoverflow.com/a/57078963/620097 . Good luck. – shellter Jul 23 '19 at 13:38
  • wrt readability of your code, naming a flag variable `flag` is as useful as naming a numeric variable `number` instead of `count`, `sum`, `total`, `average`, `max`, or whatever it represents. Don't name variables based on the type of variable they are, name them based on what they represent. In this case at a minimum you could name it `found` or even better `inBlock` or similar. – Ed Morton Jul 23 '19 at 14:25

4 Answers4

1

Could you please try following.

awk '$1==5{$1=sprintf("%.5f",3)} 1'  Input_file

EDIT: Generic one.

awk -v row=2 -v field=1 -v point="4" -v new_value=3 'FNR==row{if($field~/^[0-9]+/){$field=sprintf("%."point"f",new_value)}} 1'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

This should give the output you posted from the input you posted.

awk '/%block lattice_cart/{f=1} f {gsub("5.","3.");print} /%endblock lattice_cart/{f=0}' file
%block lattice_cart
3.00000   0.00000   0.00000
0.00000   3.00000   0.00000
0.00000   0.00000   2.3.000
%endblock lattice_cart

It will only edit value between start/stop and print all lines including start/stop.

Jotne
  • 40,548
  • 12
  • 51
  • 55
  • Yes but those values are arbitrary. I want this to be able to search for the first entry in general. Not only if it is equal to 5. – Caterina Jul 23 '19 at 11:55
  • @Caterina, you mean all first ids? Or some please be more clear on this one. – RavinderSingh13 Jul 23 '19 at 11:56
  • @RavinderSingh13 I mean I want to be able to change each value in this matrix regardless it's initial value. So for instance if I want to change entry (1,1) to 3 it shouldn't matter if it's initial value is 5.000 or 8.000 etc – Caterina Jul 23 '19 at 11:58
1
$ cat tst.awk
/%block lattice_cart/ { inBlock=1 }
inBlock {
    if ( numLines++ == row ) {
        $col = val
    }
    block = block $0 ORS
    if ( /%endblock lattice_cart/ ) {
        printf "%s", block
        inBlock = numLines = 0
    }
}

$ awk -v row=1 -v col=1 -v val=3 -f tst.awk file
%block lattice_cart
3 any_number any_number
any_number   any_number  any_number
any_number   any_number  any_number
%endblock lattice_cart

$ awk -v row=3 -v col=2 -v val=27 -f tst.awk file
%block lattice_cart
any_number   any_number  any_number
any_number   any_number  any_number
any_number 27 any_number
%endblock lattice_cart
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

Seems that

awk '/%block lattice_cart/{print;getline;$1="3"}1' pacet.cell

Does the job. And for the other entries I just add more getlines or change to the next column.

awk '/%block lattice_cart/{print;getline;print;getline;$2="3"}1' pacet.cell

It is not the cleanest way perhaps but it does the job.

Caterina
  • 775
  • 9
  • 26