Replace values

Gents,

Please can you help me with this.

When column 49 == 2

Need to do the following changes;

Change previous row field (substr$0,45,4)-1
Change previous row field (substr$0,72,5)+2
Change actual row field (substr$0,40,4)+1
Change actual row field (substr$0,49,1)-1
Change actual row field (substr$0,62,5)+12

Before

X  4714     14710  69445.00  19257.001 1218 12271  69596.00  19460.00  19478.001
X  4714     14710  69445.00  19257.001 1228 12292  69596.00  19480.00  19480.001

After

X  4714     14710  69445.00  19257.001 1218 12281  69596.00  19460.00  19480.001
X  4714     14710  69445.00  19257.001 1229 12291  69608.00  19480.00  19480.001

Thanks

Trying to understand your request, I found it doesn't fit the samples:

X  4714     14710  69445.00  19257.001 1218 12271  69596.00  19460.00  19478.001
                                            ^^^^----------------------------------(substr$0,45,4)-1
                                                                       ^^^^^------(substr$0,72,5)+2
X  4714     14710  69445.00  19257.001 1228 12292  69596.00  19480.00  19480.001
                                                ^---------------------------------if == 2
                                       ^^^^---------------------------------------(substr$0,40,4)+1 
                                                ^---------------------------------(substr$0,49,1)-1  
                                                             ^^^^^----------------(substr$0,62,5)+12

X  4714     14710  69445.00  19257.001 1218 12281  69596.00  19460.00  19480.001
                                            NNNN                       YYYYY 
X  4714     14710  69445.00  19257.001 1229 12291  69608.00  19480.00  19480.001
                                       YYYY     Y    NNN     NNNNN

"Y" indicates "might work"; "N" "doesn't"

Looks like you are overcomplicating things. Why working on characters and substrings? Can't you define it in, say, awk fields? Like if ($7 ~ /2$/) $7+=10 , and so on?

---------- Post updated at 22:03 ---------- Previous update was at 20:08 ----------

Please check if the logics deliver what you need; I took into account your input and output samples, not your verbalized request. You can easily adapt that if need be. Formatting according to your input file will take another exercise and is left to you...

awk     '               {T[NR%2]=$0}
         $7 ~ /2$/      {$6 +=1; $7-=1; $8+=12  
                         T[NR%2]=$0
                         $0=T[(NR+1)%2]
                         $7+=10; $10+=2
                         T[(NR+1)%2]=$0
                        }
                        {print T[(NR+1)%2]}
         END            {print T[NR%2]}
        ' OFS="  " CONVFMT="%.3f" file4
X  4714  14710  69445.00  19257.001  1218  12281  69596.00  19460.00  19480.001
X  4714  14710  69445.00  19257.001  1229  12291  69608  19480.00  19480.001
1 Like

Dear RudiC,

Great thanks for your help.

I fix it like this

awk     '               {T[NR%2]=$0}
         $7 ~ /2$/      {$6 +=1; $7-=1; $8+=12  
                         T[NR%2]=$0
                         $0=T[(NR+1)%2]
                         $7+=10; $10+=2
                         T[(NR+1)%2]=$0
                        }
                        {print T[(NR+1)%2]}
         END            {print T[NR%2]}
       ' OFS="  " CONVFMT="%.3f" file1.txt | awk '{
         cl1 = $1; cl2 = $2; cl3 =$3; cl4 =$4; cl5 =$5; cl6 =$6; cl7 =$7; cl8 =$8; cl9 =$9; cl10 =$10
         printf("%1s %5d %9d %9.2f %10.3f %4d %5d %9.2f %9.2f %10.3f\n", cl1,cl2,cl3,cl4,cl5,cl6,cl7,cl8,cl9,cl10)}'

Thansk again

Why, then, don't you do it like

awk     'BEGIN          {split ("%1s |%5d |%9d |%9.2f |%10.3f |%4d |%5d |%9.2f |%9.2f |%10.3f", FMT, "|")}
         function Prt_it(IX)
                        {n=split (T[IX], O)
                         for (i=1; i<=n; i++) printf FMT, O
                         printf "\n"
                        }
                        {T[NR%2]=$0}

         $7 ~ /2$/      {$6 +=1; $7-=1; $8+=12
                         T[NR%2]=$0
                         $0=T[(NR+1)%2]
                         $7+=10; $10+=2
                         T[(NR+1)%2]=$0
                        }
         NR>1           {Prt_it((NR+1)%2)}
         END            {Prt_it(NR%2)}
        ' file

?

1 Like

RudiC

You are great.. your help is greatly appreciated

Dear RudiC

Sorry to ask you again.

What i should change in the code to be able to make the changes
In the next line when the value 2 is found, and not the previous line like It is now.

I have try many things, but i cant

Thanks

That's much easier; don't need a circular buffer but just set a boolean to remember that "2" was found, modify & print the current line, if boolean set then read, modify, & print the next line ans reset the boolean.