cutting multiple columns into multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cutting multiple columns into multiple files
# 1  
Old 06-01-2012
cutting multiple columns into multiple files

Hypothetically, suppose that

file1
Code:
id v1 v2 v3 v4 v5 v6 v7..........v100
1 1 1 1 1 1 2 2 .....50
2 1 1 1 1 1 2 2 .....50
3 1 1 1 1 1 2 2 .....50
4 1 1 1 1 1 2 2 .....50
5 1 1 1 1 1 2 2 .....50

I want to write a loop such that I take the id# and the first 5 columns (v1-v5) into the file name "chrom1.txt" and take the id# and the next 5 columns (v6-v10) save it as "chrom2.txt" and so forth
So I will have 50 files with 6 columns in total.

I've been trying to "cut" command but it got little complicated when loop is incorprated.

Thanks in advance!

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 06-01-2012 at 06:05 PM..
# 2  
Old 06-02-2012
Something like this maybe:

Code:
for (( i=0;i<20;i++ )) do awk '{OFS="\t"}NR!=1{{printf($1"\t")} for (j=1;j<=5;j++) {c=j+('$i'*5)+1; printf($c"\t")}{printf("\n")}}' file1 > file_$i; done;

Here, file1 is the input file split into tab delimited data files named like file_0, file_1....

Last edited by jawsnnn; 06-02-2012 at 04:36 AM..
This User Gave Thanks to jawsnnn For This Post:
# 3  
Old 06-02-2012
Or:
Code:
awk '{for(i=2;i<=NF;i+=c){s=$1; f="chrom" (i-2)/c+1 ".txt"; for(j=0;j<c;j++)s=s OFS $(i+j); print s>f}}' OFS='\t' c=5 infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-04-2012
Thanks!!! what a life saver.

I just need to modify little bit for my problem. I hope it works Smilie

---------- Post updated at 09:52 AM ---------- Previous update was at 09:40 AM ----------

jawsnnn,
Is there any way that I could keep my headers of each field?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. UNIX for Dummies Questions & Answers

Joining different columns from multiple files

Hello again, I am trying to join 3rd column of 3 files into the end on one file and save it separately... my data looks like this file 1 Bob, Green, 80 Mark, Brown, 70 Tina, Smith, 60 file 2 Bob, Green, 70 Mark, Brown, 60 Tina, Smith, 50 file 3 Bob, Green, 50 Mark, Brown,60 Tina,... (6 Replies)
Discussion started by: A-V
6 Replies

4. Shell Programming and Scripting

Merging multiple files from multiple columns

Hi guys, I have very basic linux experience so I need some help with a problem. I have 3 files from which I want to extract columns based on common fields between them. File1: --- rs74078040 NA 51288690 T G 461652 0.99223 0.53611 3 --- rs77209296 NA 51303525 T G 461843 0.98973 0.60837 3... (10 Replies)
Discussion started by: bartman2099
10 Replies

5. Shell Programming and Scripting

Compare multiple files with multiple number of columns

Hi, input file1 abcd 123 198 xyz1:0909090-0909091 ghij 234 999 xyz2:987654:987655 kilo 7890 7990 xyz3:12345-12357 prem 9 112 xyz5:97-1134 input file2 abcd 123 198 xyz1:0909090-0909091 -9.122 0 abed 88 98 xyz1:98989-090808 -1.234 1.345 ghij 234 999 xyz2:987654:987655 -10.87090909 5... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. Web Development

mysql query for multiple columns from multiple tables in a DB

Say I have two tables like below.. status HId sName dName StartTime EndTime 1 E E 9:10 10:10 2 E F 9:15 10:15 3 G H 9:17 10:00 logic Id devName capacity free Line 1 E 123 34 1 2 E 345 ... (3 Replies)
Discussion started by: ilan
3 Replies

8. Shell Programming and Scripting

Cutting a file with multiple delimiters into columns

Hi All I have recently had to start using Unix for work and I have hit brick wall with this prob.... I have a file that goes a little something like this.... EUR;EUR;EUR:USD:USD;USD;;;EUR/USD;XAU/AUD;XAU/EUR;XAU/AUD,GBP/BOB,UAD/XAU;;;1.11;2.22;3.33;4.44;5.55;6.66;;; is it possible to... (7 Replies)
Discussion started by: luckycharm
7 Replies

9. Shell Programming and Scripting

need help with post:extract multiple columns from multiple files

hello, I will would be grateful if anyone can help me reply to my post extract multiple cloumns from multiple files; skip rows and include filenames; awk Please see this thread. Thanks manishabh (0 Replies)
Discussion started by: manishabh
0 Replies

10. Shell Programming and Scripting

Combine multiple columns from multiple files

Hi there, I was wondering if someone can help me with this. I am trying the combine multiple columns from multiple files into one file. Example file 1: c0t0d0 c0t2d0 # hostname vgname c0t0d1 c0t2d1 # hostname vgname c0t0d2 c0t2d2 # hostname vgname c0t1d0 c0t3d0 # hostname vgname1... (5 Replies)
Discussion started by: martva
5 Replies
Login or Register to Ask a Question