Printing second column of several files into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing second column of several files into one
# 1  
Old 10-27-2011
Printing second column of several files into one

HI All,

I have exactly 100 text files with extension .txt. The files contain numbers like this:

1.txt

Code:
0.4599994 65914
0.40706193 190743
0.39977244 185019
0.39831382 74906
0.3915928 122428
0.38844505 39999
0.38820446 72691
0.38787442 176430
0.38670844 28791
0.38597047 91091
0.38308746 37638

I want to extract all the second columns from all 100 files and store in 1 single file. But this has to be done sequentially starting from 1 to 100 not in any order.

But also after extracting after one file, I need 2 newlines in the resulting file so that I may know where the first file ends and the second one starts. Like this

output_file.dat

Code:
92839232
328932
589458495
212123
454


4343
23232
46565
43

The above output shows that after 1.txt has been extracted, I print two newlines before starting with 2.txt and keep going so on.

I know how to extract the second columns using this

Code:
awk '{print $2}' 1.txt

But I am not able to run it for 100 files. I am using Linux with BASH.
# 2  
Old 10-27-2011
Code:
$ i=1; while [ $i -le 100 ];do cut -d' ' -f2 < ${i}.txt; i=$((i+1)); echo ""; echo ""; done > outfile

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 10-27-2011
Code:
> new
for i in {1..100}.txt
do  
    awk '{print $2}END{printf RS RS}' $i >> new 
done

This User Gave Thanks to rdcwayx 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

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

2. Shell Programming and Scripting

Inconsistent column printing

Hi, I have a file that has inconsistently numbered columns. Like row1 has 23 columns, and row 2 has 34 columns etc. I would like to re-order the first 8 columns as required and from the 9th column till the end, I would like to print it as it is. I tried to read the re-ordered 8 columns... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

3. Shell Programming and Scripting

printing certain elelment of a column

"File1.txt" CHR SNP BP ANNOT 8 rs1878374 127974042 MYC(-843.5kb)|FAM84B(+334.4kb) 2 rs2042077 16883103 VSNL1(-702.2kb)|SMC6(-825.5kb)|RAD51AP2(-672.4kb)|MYCN(+878.5kb)|MSGN1(-978.2kb)|GEN1(-915.6kb)|FAM49A(+172.5kb) 12 rs10431347 3023955... (4 Replies)
Discussion started by: johnkim0806
4 Replies

4. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

5. Solaris

Printing a particular column[autosys]

Dear All, I'm using autosys in my production system. My concern is as follows: autosys -j <some_job_nm> Output: Job Name Last Start Last End ST Run Pri/Xit ... (1 Reply)
Discussion started by: saps19
1 Replies

6. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

7. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

8. UNIX for Dummies Questions & Answers

creating a file using the fist column and printing second column

Hello all. I have a problem that I need help solving. I would like to convert the following file: human pool1_12 10e-02 45 67 human pool1_1899 10e-01 45 29 human pool1_1829 10e-01 43 26 horse pool1_343 10e-20 65 191 horse pool1_454 10e-09 44 43... (5 Replies)
Discussion started by: viralnerd
5 Replies

9. Shell Programming and Scripting

regarding about printing row to column

Hello, I got data like that, =111 A= alpha B= 1 C= qq D= 45 F= ss G= 334 =1234 A= B= 2w C= D= 443 F= G= =3434 A= B= e3e (5 Replies)
Discussion started by: davidkhan
5 Replies

10. UNIX for Dummies Questions & Answers

Printing highest value from one column

Hi, I have a file that looks like this: s6 98 s6 91 s6 56 s5 32 s5 10 s5 4 So what I want to do is print only the highest value for each value in the column: So the file will look like this: s6 98 s5 32 Thanks (4 Replies)
Discussion started by: phil_heath
4 Replies
Login or Register to Ask a Question