Paste second columns from files to its corresponding file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Paste second columns from files to its corresponding file
# 1  
Old 10-28-2011
Paste second columns from files to its corresponding file

Hi All,

I have two sets of files.
One set with extension .txt This set has file names with numbers like these. 1.txt, 2.txt, 3.txt until extactly 100.txt.

The .txt files look like these:


Code:
0.38701788 93750
0.38622013 94456
0.38350296 94440
0.38282126 94057
0.38282126 94439
0.35847232 166486
0.3557739 167627
0.35375914 156042
0.35018 144546
0.3486718 167493

I have another set of files with extension .dat which also have 1.dat, 2.dat, 3.dat until 100.dat. In this case every 1.txt has 1.dat file and 2.txt has 2.dat file.

The .dat files look like these:

Code:
1
3
4
5
3
2
4
5
6
4

The line number of 1.dat and 1.txt are all same. Same goes for other files too.

What I want is to extract the second column of .txt files and "paste" them in dat files like this, so that my resulting .dat file looks like this.:


Code:
93750	1
94456	3
94440	4
94057	5
94439	3
166486	2
167627	4
156042	5
144546	6
167493	4

I have to do this for all 100 files. This means second column of 1.txt is pasted with 1.dat, second column of 2.txt is pasted in 2.dat and so on until 100.

I can paste the entire .txt file using my code, but unable to do it for the second column only. I am using Linux.

Code:
for num in `seq 1 100`; do
 paste $num.txt $num.dat >$num.comb
done

# 2  
Old 10-28-2011
Try this...
Code:
#!/bin/bash
for file in *.txt
do
        num=${file%%.*}
        paste $num.txt $num.dat |awk '{$2=""}1' > $num.comb
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk command to paste 1st columns of 2 files.

I have 2 files contains more than 5000 lines, I want to paste the 1st column of both file in an output file using awk. Please suggest to me I had tried paste command but it merges my both column. --- Post updated at 10:26 PM --- file1.txt 2020-01-07 235400 2020-01-07 235400 2020-01-07... (4 Replies)
Discussion started by: Sagar Singh
4 Replies

2. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

3. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

4. UNIX for Beginners Questions & Answers

Character screening and paste into newfile in columns instead of rows

QL10169_SAUJANA%SubNetwork=ONRM_ROOT_MO_R,SubNetwork=ERBS_KCRN11,MeContext=QL10169_SAUJANA_5 %External_Link_Failure %X2_link_problem_to_one_or_several_neighbouring_eNodeBs. QL10187_MATANG_JAYA_2%SubNetwork=ONRM_ROOT_MO_R,SubNetwork=ERBS_KUCHING,MeContext=QL10187_MATANG_JAYA_2_3... (2 Replies)
Discussion started by: Ankit Vyas
2 Replies

5. Shell Programming and Scripting

Character screening and paste into new file in columns instead of rows

QL10169_SAUJANA%SubNetwork=ONRM_ROOT_MO_R,SubNetwork=ERBS_KCRN11,MeContext=QL10169_SAUJANA_5 %External_Link_Failure %X2_link_problem_to_one_or_several_neighbouring_eNodeBs. QL10187_MATANG_JAYA_2%SubNetwork=ONRM_ROOT_MO_R,SubNetwork=ERBS_KUCHING,MeContext=QL10187_MATANG_JAY A_2_3... (2 Replies)
Discussion started by: Ankit Vyas
2 Replies

6. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

7. Shell Programming and Scripting

Paste many files with different row number in one file

Hi everybody I am trying to merge many files in one files using paste or pr command but I am not able to resolve this issue. The file are tab delimited like these: 1 4721519 4723118 1 5022468 5024918 1 7093519 7098118 2 19736573 19741172 2 21907973 21910572... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

8. Shell Programming and Scripting

Copy and Paste Columns in a Tab-Limited Text file

I have this text file with a very large number of columns (10,000+) and I want to move the first column to the position of the six column so that the text file looks like this: Before cutting and pasting ID Family Mother Father Trait Phenotype aaa bbb ... (5 Replies)
Discussion started by: evelibertine
5 Replies

9. Shell Programming and Scripting

appending several columns with awk and paste

Hello, I am trying to solve for a couple of hours now the following problem: I have n files and would like to add the third column of each file to a new file: temp1.txt 1 2 3 1 2 3 1 2 3 temp2.txt 1 2 4 1 2 4 1 2 4 1 2 4 temp3.txt (2 Replies)
Discussion started by: creamcheese
2 Replies

10. UNIX for Dummies Questions & Answers

cut and paste columns using awk

Hi, Let's say that I have a file called table, I know that if I need to see a the second column for exampls I use: awk ' {print $2}' table.txt Is there anyway to use awk to actually cut a column and put it somewhere else in the table?:confused: (8 Replies)
Discussion started by: cosmologist
8 Replies
Login or Register to Ask a Question