Replace values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace values
# 1  
Old 05-23-2015
Replace values

Gents,

Can you please help with this.

Input file
Code:
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
49801 2012 2
49803 2012 2
49973 2012 170
49975 2012 2
49977 2012 2
49979 2012 2

output desired.
Code:
49955 2009 V2
49957 2010 V2
49959 2010 V2
49961 2010 V2
49963 2010 V2
49789 2011 V3
49791 2011 V3
49793 2011 V3
49795 2011 V3
49965 2011 V2
49967 2011 V2
49969 2011 V2
49971 2011 V2
49797 2012 V3
49799 2012 V3
49801 2012 V3
49803 2012 V3
49973 2012 V2
49975 2012 V2
49977 2012 V2
49979 2012 V2

I would like to replace values in column 3 with values V2 and V3, as example of output file, When the value in column 3 = -174, need to replace to V3 and also the next 3 rows.

Thanks
# 2  
Old 05-23-2015
Hi, try:
Code:
awk '$3<0{c=4} {$3=(c-->0)?"V3":"V2"}1' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-23-2015
Dear Scrutinizer

Kindly can u explain this part
Code:
{$3=(c-->0)?

of the code Smilie
# 4  
Old 05-23-2015
Hi Jiam,
Code:
$3=(c-->0)?"V3":"V2"

means if c>0 then make $3 equal to V3 else V2 and c-- means decrement c by 1 one after use of the variable

To illustrate:
Code:
$ awk 'BEGIN{c=4; print c--; print c--; print c; print c}'
4
3
2
2

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-23-2015
Great, thanks a lot

---------- Post updated at 02:26 PM ---------- Previous update was at 01:57 PM ----------

Sorry to ask again
using the same output file. How to replace the value in the 3 column for the last 5 rows of the file, with value V7
Example
Code:
49955 2009 V2
49957 2010 V2
49959 2010 V2
49961 2010 V2
49963 2010 V2
49789 2011 V3
49791 2011 V3
49793 2011 V3
49795 2011 V3
49965 2011 V2
49967 2011 V2
49969 2011 V2
49971 2011 V2
49797 2012 V3
49799 2012 V3
49801 2012 V3
49803 2012 V7
49973 2012 V7
49975 2012 V7
49977 2012 V7
49979 2012 V7



Thanks and regards
# 6  
Old 05-23-2015
You 'll need to use a ring buffer, then. Try
Code:
awk '
$3<0    {c=4}
        {$3=(c-->0)?"V3":"V2"
         RB[NR%6]=$0 
        }
NR>=6   {print RB[(NR+1)%6] 
        }
END     {for (t=2; t<=6; t++)   {TMP=RB[(NR+t)%6]
                                 print substr (TMP,1, length(TMP)-1) "7"
                                }
        }
' file
49955 2009 V2
49957 2010 V2
49959 2010 V2
49961 2010 V2
49963 2010 V2
49789 2011 V3
49791 2011 V3
49793 2011 V3
49795 2011 V3
49965 2011 V2
49967 2011 V2
49969 2011 V2
49971 2011 V2
49797 2012 V3
49799 2012 V3
49801 2012 V3
49803 2012 V7
49973 2012 V7
49975 2012 V7
49977 2012 V7
49979 2012 V7

---------- Post updated at 22:56 ---------- Previous update was at 22:50 ----------

Or, if available on your system, use the tac utility:
Code:
awk '$3<0{c=4} {$3=(c-->0)?"V3":"V2"}1' file | tac | awk 'NR<=5 {$NF="V7"} 1' | tac

Due to the process creation overhead, this is about three times slower with your sample data; for larger data set this ratio drops considerably.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-24-2015
Dear RudiC.
Thanks a lot for your help
It works fine.. I use te second option with tac utility Smilie
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, 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... (6 Replies)
Discussion started by: jiam912
6 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