Reshuffle values in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reshuffle values in a column
# 1  
Old 03-13-2014
Reshuffle values in a column

Hi,

I would like to know how can I reshuffle values in a column from a text file.

I know that shuf command does the trick of reshuffling values but how to substitute lets say the original values of column 9 of a text file by the reshuffling values?

Thanks in advance
# 2  
Old 03-13-2014
Can you please provide sample data?
# 3  
Old 03-13-2014
Sure.

File1

Column1 Column2
Code:
a 1
b 1
c 1
d 2
e 2
f 2

Desired file is to have the values of column 2 shuffled like:
Code:
a 2
b 1
c 1
d 1
e 2
f 2

Thanks.

Last edited by vgersh99; 03-13-2014 at 02:01 PM.. Reason: code tags, please!
# 4  
Old 03-13-2014
awk -f fadista.awk myInputFile where fadista.awk is:
Code:
function genrand(n)
{
   return (int(n*rand())+1);
}

BEGIN {
   srand()
}
{ if (!($2 in vA)) {vA[$2]; v[++vN]=$2}
  a[FNR]=$1
}
END {
  for(i=1; i in a;i++)
    print a[i], v[int(genrand(vN))]
}

# 5  
Old 03-13-2014
Thanks but this only works if you have two columns. What should I change in your script to be able to output all the columns, lets say of a file with 20 columns, in which the column I want to shuffle is the 9th.
# 6  
Old 03-13-2014
Code:
awk -f fadista.awk myInputFile
OR
for the 9-th column
awk -v c=9 -f fadista.awk myInputFile


Code:
function genrand(n)
{
   return (int(n*rand())+1);
}

BEGIN {
   if (!c) c=2;
   srand()
}
{ if (!($c in vA)) {vA[$c]; v[++vN]=$c}
  a[FNR]=$0
}
END {
  for(i=1; i in a;i++) {
    n=split(a[i], A)
    for(j=1;j<=n;j++)
       printf("%s%c", (j==c)? v[int(genrand(vN))]:A[j], (j==n)?ORS:OFS)
  }
}

This User Gave Thanks to vgersh99 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

6. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

7. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question