-2

I want to scramble 2nd and 4th column of the data.

[1234567890] -> [8728125870]

Input:

 First_name,id,second_name,phone_number
 ram,232,prakash,96
 hari,123,pallavi,98
 anurag,456,aakash,13

Output Expected:

 First_name,id,second_name,phone_number
 ram,727,prakash,82
 hari,872,pallavi,78
 anurag,812,aakash,87
vinay24
  • 3
  • 3

2 Answers2

0

Step 1:

Generate a random number to be used for replacement. This is done with something similar to $(($RANDOM * 32768 + $RANDOM)); if you need more details, let me know.

Step 2:

Use tr to replace all numbers with their "capital" versions, and replace them with the numbers you need. So for your example, to replace just the 1, 2, and 3, you would run:

cat inputfile | tr '1' '!' | tr '2' '@' | tr '3' '#' | tr '4' '$' | tr '5' '%' | tr '6' '^' | tr '7' '&' | tr '9' '(' | tr '!' '8' | tr '@' '7' | tr '#' '2' | tr '$' '8' | tr '%' '1' | tr '^' '2' | tr '&' '5' | tr '(' '7'

Note: to get the effect you need, you would need to extend the command above for it to work. Running it multiple times insted will produce the wrong results.

Output of the above command:

First_name,id,second_name,phone_number
 ram,727,prakash,96
 hari,872,pallavi,98
 anurag,456,aakash,87

Also: note how your "expected" output is not correct. The first number should be 727, not 728.

Alex
  • 947
  • 1
  • 8
  • 25
  • hi Alex, Thanks for the answer. I dont want any randomised way. when ever i have 1234567890 numbers in my data thos have to scrambled into 8728125870 respectivly. Can you please try to give full length code. – vinay24 Aug 24 '17 at 06:30
  • OK, let me know if this does what you want. – Alex Aug 24 '17 at 14:04
0
 BEGIN { FS=OFS="," }
    {
      cmd = "echo '" $0 "' | tr '1234567890' '8728125870'"
      new = ( (cmd | getline line) > 0 ? line : $1 )
      close(cmd)
      split(new,tmp)
      for (i in tmp) {
          if (i ~ /^(2|4)$/) {
              $i = tmp[i]
                     }
     }
      print
   }

Save the above code in test.awk file and execute using -> awk -f test.awk datanew.csv

vinay24
  • 3
  • 3