How to shuffle odd and even columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to shuffle odd and even columns?
# 1  
Old 10-13-2015
How to shuffle odd and even columns?

Is there any way to place each even column name infront of its odd column using awk or others?

input

Code:
Ab	name	MGH26	B04	MGH26	B05

output

Code:
name_Ab	B04_MGH26	B05_MGH26

# 2  
Old 10-13-2015
off the top of my head:

Code:
awk '{
for (i=1;i<=NF;i++) {
    if (i%2) {
        oname=$i
    } else {
        ename=$i
        printf("%s_%s ",ename,oname);
    }
}

Doesn't preserve the white spacing though.
This User Gave Thanks to cjcox For This Post:
# 3  
Old 10-13-2015
it works great. thanks.
note:you forgot this at end of the script.
Code:
}'

# 4  
Old 10-13-2015
Code:
awk 'for (i=1;i<=NF; i=i+2) printf("%s%s", $(i+1) "_" $i, (i+2)<NF?FS:ORS)' myFile

# 5  
Old 10-14-2015
The last solution is missing the { }
Code:
awk '{for (i=2;i<=NF;i+=2) printf "%s_%s%s", $i, $(i-1), (i<NF)?FS:ORS}' myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Logic shuffle

Hi, Is there any way I can shuffle the numbers randomly. I have been trying to google and I found lots of 'generator' but is it possible to find the background logic to create randomness? Thanks, (3 Replies)
Discussion started by: Indra2011
3 Replies

2. Shell Programming and Scripting

Randomized shuffle words on each line

Hi Folks, I have a text file with a thousand lines consisting of words or a group of words separated by commas. I would like to randomize / shuffle the words on each line. Eg; file.txt Linux,Open,Free,Awesome,Best Things in Life,The Greatest Laptop,PC,Tablet,Home Computers,Digital... (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Shuffle Columns in Pipe delimited data

My sample data contains escape characters followed by delimiter. I'm stuck in writing awk comand to swap the columns. please help me out. Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"| Expected Output Data: 12345678|C|ABN\|XYZ MED CHEM PTY.... (10 Replies)
Discussion started by: BrahmaNaiduA
10 Replies

4. Shell Programming and Scripting

Random shuffle of lines of a TXT file

Hello friends, I have a TXT file with 300 lines in it. I need to shuffle all the lines (randomly) so that they get into different order. Can anyone pls provide easy way, if any? I got it done by doing this below but I see it very lengthy/inefficient way. call random function to generate... (2 Replies)
Discussion started by: prvnrk
2 Replies

5. Homework & Coursework Questions

perl script card shuffle

Hi, I have a problem that pertains to using perl script pop,shift,push only to shuffle a card and print out 5 top listed numbers. HW problem from oreilly school of technology linux/unix course instructor name kelly hoover #!/usr/bin/perl @startingdeck = ("A H","2 H","3 H","4 H","5... (1 Reply)
Discussion started by: flyganji
1 Replies

6. Shell Programming and Scripting

randomly shuffle two text files the same way

What I have are two text files that I need to shuffle randomly, but I need the two files to be randomly shuffled the same way. I have heard of shuf but I do not know how to use it for two files. Maybe there is also an easy/simple awk command I do not know about that could handle this problem. ... (3 Replies)
Discussion started by: adrunknarwhal
3 Replies

7. Shell Programming and Scripting

shuffle pack of words in line

hello i just seeking for a simple way to make a shuffle by block of words in a line. no matter shell (sh/bash) or perl should be like this: the message (which is line of some file) splits to packs (packs are random 5-10 words in each) then making a new line inserting those packs in a random... (9 Replies)
Discussion started by: tip78
9 Replies
Login or Register to Ask a Question