|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
@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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Tags |
| awk, random number, replace, sed |
| Thread Tools | Search this Thread |
| 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 |
|
|