Replace column by random number addition | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Replace column by random number addition

Shell Programming and Scripting


Tags
awk, random number, replace, sed

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 October 2014, 6:50 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
Replace column by random number addition

Here is my problem:-

I have a file with pipe separated values.


Code:
CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0
CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3
CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0
CR|20121021|079|ABC|N|DLS|00038|DLS|05158|2071690102|804|801|3
CR|20121021|079|ABC|N|DLS|00038|DLS|05858|589505306|473|473|0
CR|20121021|079|ABC|N|DLS|00038|DLS|26456|-1482184796|371|371|0
CR|20121021|079|ABC|N|DLS|00038|DLS|30036|1549556811|2001|1982|19
CR|20121021|079|ABC|N|DLS|00038|DLS|30038|1330597704|2460|24600|0
CR|20121021|079|ABC|N|DLS|00038|DLS|30130|218959107|1908|1894|14
CR|20121021|079|ABC|N|DLS|00038|DLS|30266|2071690102|468|468|0

Now I want to replace field: 10 (highlighted) by adding a random number to field number: 11. But the random number has to be generated based on decimal places of field: 11

E.g: In the first record, the 11th field value is 634234, so we have to generate a 4 digit random number and add to field number 11.

Hence if 11th field is

6 digits - random number should be 4 digits
5 digits - random number should be 3 digits
4 digits - random number should be 2 digits
3 digits - random number should be 1 digits

Please assist.
Sponsored Links
    #2  
Old 10-23-2012
msabhi's Avatar
msabhi msabhi is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 2 November 2013, 1:26 AM EDT
Location: In books/UNIX.com
Posts: 177
Thanks: 16
Thanked 61 Times in 60 Posts

Code:
 perl -F'\|' -alne '{$num=sprintf("9"x(length($F[10])-2));$F[9]=$F[10]+int(rand($num));$"="|";print "@F";}' input_file

The Following User Says Thank You to msabhi For This Useful Post:
Yoda (10-23-2012)
Sponsored Links
    #3  
Old 10-23-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 October 2014, 6:50 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
@msabhi, It works! Thank you very much.

One last request, if the 11th field digit is less than or equal to 3 digits I still want to generate a 1 digit random number and add it.

Can you please help with that?
    #4  
Old 10-23-2012
msabhi's Avatar
msabhi msabhi is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 2 November 2013, 1:26 AM EDT
Location: In books/UNIX.com
Posts: 177
Thanks: 16
Thanked 61 Times in 60 Posts

Code:
perl -F'\|' -alne '{$num=(length($F[10])<=3)?9:sprintf("9"x(length($F[10])-2));$F[9]=$F[10]+int(rand($num));$"="|";print "@F";}' input_file

The Following User Says Thank You to msabhi For This Useful Post:
Yoda (10-23-2012)
Sponsored Links
    #5  
Old 10-23-2012
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts

Code:
awk 'BEGIN {FS=OFS="|"; CONVFMT="%.0f"; srand()} {$10 += int(rand()*10^((x=length($11)-2)>0?x:1))} 1'

Regards,
Alister

Last edited by alister; 10-23-2012 at 09:04 PM..
The Following User Says Thank You to alister For This Useful Post:
Yoda (10-24-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to generate random number as as the first column of a txt file forevertl UNIX for Dummies Questions & Answers 2 10-22-2012 02:09 PM
Replace 2nd column for each line in a csv file with fixed string+random number tententen Shell Programming and Scripting 7 10-27-2011 11:54 AM
Using SED to replace a random number? JayC89 Shell Programming and Scripting 0 06-30-2010 05:16 AM
replace all numbers in column with another number in bash goodbenito UNIX for Dummies Questions & Answers 4 04-15-2010 09:28 AM
Sed to replace second number in a random string glev2005 UNIX for Dummies Questions & Answers 8 12-29-2009 10:45 AM



All times are GMT -4. The time now is 12:43 AM.