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:

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:

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.:

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.

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

Try this...

#!/bin/bash
for file in *.txt
do
        num=${file%%.*}
        paste $num.txt $num.dat |awk '{$2=""}1' > $num.comb
done

--ahamed

1 Like