Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-01-2012
Registered User
 
Join Date: Jun 2012
Posts: 56
Thanks: 30
Thanked 1 Time in 1 Post
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:
Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 06-01-2012 at 05:05 PM..
Sponsored Links
    #2  
Old 06-02-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts
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 03:36 AM..
The Following User Says Thank You to jawsnnn For This Useful Post:
johnkim0806 (06-04-2012)
Sponsored Links
    #3  
Old 06-02-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,338
Thanks: 143
Thanked 1,754 Times in 1,591 Posts
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

The Following User Says Thank You to Scrutinizer For This Useful Post:
johnkim0806 (06-04-2012)
    #4  
Old 06-04-2012
Registered User
 
Join Date: Jun 2012
Posts: 56
Thanks: 30
Thanked 1 Time in 1 Post
Thanks!!! what a life saver.

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

---------- 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?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Concatenate columns from multiple files newbie83 Shell Programming and Scripting 2 02-28-2012 06:07 PM
mysql query for multiple columns from multiple tables in a DB ilan Web Programming 3 07-07-2011 11:14 AM
Cutting a file with multiple delimiters into columns luckycharm Shell Programming and Scripting 7 04-02-2010 06:29 AM
need help with post:extract multiple columns from multiple files manishabh Shell Programming and Scripting 0 08-19-2009 01:09 PM
Combine multiple columns from multiple files martva Shell Programming and Scripting 5 12-02-2008 08:23 AM



All times are GMT -4. The time now is 03:02 PM.