Dividing up a text file by columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dividing up a text file by columns
# 1  
Old 09-07-2012
Dividing up a text file by columns

Hi,

I have a space de-limited text file with 1000 columns. I want to divide it up into 20 text files each with 50 columns, starting from the first column. How do I go about doing that? Thanks!
# 2  
Old 09-07-2012
Quote:
Originally Posted by evelibertine
Hi,

I have a space de-limited text file with 1000 columns. I want to divide it up into 20 text files each with 50 columns, starting from the first column. How do I go about doing that? Thanks!
I really like this questionSmilie

Try this...

Here it will create files named as good_50,good_100, and so on..

Code:
awk '{for ( i=1;i<=NF;i++){if(i%50){p=p" "$i}else {s="good_"i;p=p" "$i;print p > s;p=""}}}' file

Hope it helps...Smilie

Last edited by pamu; 09-08-2012 at 07:18 AM..
# 3  
Old 09-08-2012
The colrm command - if available on your system, which, alas, you do not mention - would do the job, but you would need to run it 20 times. pamu's suggestion is a good starting point, but needs some refinement. Try this, it might do what you want:
Code:
awk     '{tmp=""; fn="file_0";
          for ( i=1;i<=NF;i++)
                {tmp=tmp $i OFS;
                 if (!(i%50)) {print substr(tmp,1,length(tmp)-1) > fn; tmp=""; fn="file_"i; fn}
                }
         }' infile


Last edited by RudiC; 09-08-2012 at 10:27 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies

2. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

3. Shell Programming and Scripting

Dividing one file into several files

Hello, I am using Awk in UBUNTU 12.04. I have a file as follows, the first column represents the name of populations, in my real file, I have 47 populations and 2172 lines in total with about 47000 fields. ABO_1 1 2 ABO_1 1 2 ABO_2 1 1 GPO_1 1 1 GPO_1 2 2 GPO_2 1 0 GPO_2 2 0 I want... (3 Replies)
Discussion started by: Homa
3 Replies

4. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

Dividing file into columns

Hi, I have a file containing 28048 lines and I would like to split it into a a file with 3506 lines and 8 columns? In column 1, I would like to have the first 3506 lines, in columns 2 the second 3506 lines and so on. I've been looking around in the forum but I found very specific cases. Is... (1 Reply)
Discussion started by: f_o_555
1 Replies

6. UNIX for Dummies Questions & Answers

Dividing all columns by the nth row

Hi All! if I have a file like this: 8 10 12 14 16 18 0 2 6 2 4 6 8 10 12 16 18 10 6 8 12 16 18 0 2 2 6 2 2 2 2 2 2 2 2 2 10 12 16 4 8 16 4 16 0 8 10 14 16 0 4 8 12 14 And I want to divide all the lines by line 4 for example... (6 Replies)
Discussion started by: cosmologist
6 Replies

7. UNIX Desktop Questions & Answers

COMPARING COLUMNS IN A TEXT FILE

Hi, Good day. I currently have this data called database.txt and I would like to check if there are no similar values (all unique) on an entire row considering the whole column data is unique. the data is as follows cL1 cL2 cL3 cL4 a12 c13 b13 c15 b11 a15 c19 b11 c15 c17 b13 f14 with... (1 Reply)
Discussion started by: whitecross
1 Replies

8. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

9. Shell Programming and Scripting

how to extract columns from a text file

Hi, In ksh, I have a file with similar rows as follows: Department = 1234 G/L Asset Acct No = 12.0000. 2/29/2008 Department = 1234 G/L Asset Acct No = 13.0000. 3/29/2008. I want to create a new text file that contains only the numbers and date: 1234 12.0000. 2/29/2008 1234 13.0000. ... (16 Replies)
Discussion started by: ihot
16 Replies

10. Programming

dividing a file into two

Hi .. I have a file which contains a list of numbers like.. 1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025,2026,2027, 2028,2029,2030,2031,2032,2115,2116,2117,2118,2119,2120,2121,2122,2123,2124,2125,2126,2127,2128,2129,2130,2131,2132,... (2 Replies)
Discussion started by: jazz
2 Replies
Login or Register to Ask a Question