Replace values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace values
# 1  
Old 02-19-2015
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
Code:
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
Code:
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
# 2  
Old 02-19-2015
Trying to understand your request, I found it doesn't fit the samples:
Code:
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...
Code:
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

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-20-2015
Dear RudiC,

Great thanks for your help.

I fix it like this
Code:
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
# 4  
Old 02-20-2015
Why, then, don't you do it like
Code:
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[i], O[i]
                         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

?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-20-2015
RudiC

You are great.. your help is greatly appreciated
# 6  
Old 02-21-2015
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
# 7  
Old 02-21-2015
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values on file

Gents, Please i need your help. Using the file2.txt i will like to replace values in file3.txt. Example in file 2 column 1 is the value to find in file3.txt and replace with value in colunm2 (file2.txt). Example file2.txt 21 1209 22 1210file3.txt SCI TB Timestamp Local : 8/30/17... (2 Replies)
Discussion started by: jiam912
2 Replies

2. Shell Programming and Scripting

To transpose and replace the values

Hi Guys, I am having a file like below AZ_H,NZ_K,IN_F,AZ1_A output required AZ_H string, NZ_K int, IN_F int, AZ1_A double the values will starting like below _A - double _F - int _H - string _K - int (6 Replies)
Discussion started by: rohit_shinez
6 Replies

3. Shell Programming and Scripting

Replace values using other file

Gents, Please can you help me. I need to update file1 using file2 values file1 S 44519.00 49349.00 1V1 0.0 0 0.0 0.0 0.0 0.0289091513 S 44513.00 48581.00 1V1 0.0 0 0.0 0.0 0.0 0.0289094319 S 44511.00 48605.00 1V1 0.0 0 0.0... (1 Reply)
Discussion started by: jiam912
1 Replies

4. Shell Programming and Scripting

Replace values

Gents, Can you please help with this. Input file 49955 2009 2 49957 2010 2 49959 2010 2 49961 2010 2 49963 2010 2 49789 2011 -174 49791 2011 2 49793 2011 2 49795 2011 2 49965 2011 170 49967 2011 2 49969 2011 2 49971 2011 2 49797 2012 -174 49799 2012 2 (8 Replies)
Discussion started by: jiam912
8 Replies

5. Shell Programming and Scripting

Replace values in columns

I have the following input format: AA00000712000 -0.17 0.90 -1.04 -0.37 -1.45 -1.13 -0.22 -0.34 -0.55 2.37 0.67 -0.48 -0.48 AA00000712001 0.15 0.03 0.47 0.62 2.04 1.14 0.29 -0.81 0.85 0.53 1.00 -0.10 -0.48 BB00000712000 1.32 -0.47 0.44 0.00 0.98 ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

6. Shell Programming and Scripting

Replace values between 2 files

I want to replace the third and fourth lines of a 2nd file by the first two lines of a file. Input: file_1 file_1.line_1 file_1.line_2 file_2 file_2.line_1 <file_2.line_2_blank> file_2.line_3 file2.line_4 <file_2.line_5_blank> Output: file_2.line1 <file_2.line_2_blank>... (1 Reply)
Discussion started by: arpagon
1 Replies

7. Shell Programming and Scripting

Find and replace many values

Dear Friends, I did the same question before in other thread, but I want to explain a little better my request. I am looking for a way how to find and replace a values in two files using a reference a file where are the key to replace. Basically, I want to keep a copy of the original file... (1 Reply)
Discussion started by: jiam912
1 Replies

8. UNIX for Dummies Questions & Answers

using sed to replace values...

I have a file: $somevar=somevalue $anothervar= $someothervar=45 I'd like to be able to replace the values in the file. I just don't know how exactly to use sed. I was thinking along the lines of: cat file | sed "s/$somevar=/anotherval/g" I was hoping this would make the... (2 Replies)
Discussion started by: mrwatkin
2 Replies

9. Web Development

Replace xml values

Hallo all, I try to create a bash script but till now without any positiv results. The script should replace different variables in a text file with the right xml values Look at the following xml file: file.xml =================================== <?xml version="1.0"... (1 Reply)
Discussion started by: research3
1 Replies

10. Shell Programming and Scripting

replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC ' NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code. ... (6 Replies)
Discussion started by: charandevu
6 Replies
Login or Register to Ask a Question