Stack Exchange
copy and paste for columns between two text files
I have two text files. I need to copy columns 5-6-7 of one text file and paste these columns to another text file. For example, first column of text files as follows;
text_1:
E GPS41 568026000 0.000000000000000E+00 1.097676240456553E+04 -1.668403004627044E+04 1.770513486005421E+04 2.140299562139315E+00 -5.351523203487980E-01 -1.876979500368582E+00 1.714052588162549E-05 1.390618031795355E-05 8.605165964871206E-06 3.266367837880071E-09 3.392831966908295E-09 1.626218351208862E-09

text_2:
E    GPS41  0  5.68026000000000E+08  1.09767623810000E+04 -1.66840300460000E+04  1.77051348440000E+04

I need to copy column 5-6-7 of text_1 and paste these to text_2 as follow;
new_text:
E    GPS41  0  5.68026000000000E+08 1.097676240456553E+04 -1.668403004627044E+04 1.770513486005421E+04 


Do you need to match the corresponding lines (based on the value of the first couple of fields, say) or is it a simple side-by-side paste of line 1 from file 1  with line 1 from file 2 and so on?

just a simple side by side paste.

So you want to replace fields 5-7 of text_2 with those of text_1?

yes it is correct.

Since your data appears to have inconsistent amounts of whitespace between fields, awk will handle it better than cut. So paste the two files together and then pick out the required fields from the composite:
paste text_2 text_1 | awk '{print $1,$2,$3,$4,$12,$13,$14}' > new_text 

Ex.
$ cat new_text 
E GPS41 0 5.68026000000000E+08 1.097676240456553E+04 -1.668403004627044E+04 1.770513486005421E+04

The output differs from yours only in terms of whitespace:
$ diff -sb your_new_text new_text 
Files your_new_text and new_text are identical

If you need to preserve the exact amounts of whitespace, you can change the awk print to a suitably formatted printf.

You can do it with cut and paste if you first squeeze the delimiters down to single spaces:
paste -d ' ' <(tr -s ' ' < text_2 | cut -d ' ' -f 1-4) <(tr -s ' ' < text_1 | cut -d ' ' -f 5-7) > new_text


Dear @steeldriver, if I want to copy column 5-6-7 of text_2 into text_1 while preserving text_1 other columns, how can you modify the code?

I will ask this as a new question.

OK - can I suggest you provide a simpler test case (with simpler, shorter fields - and preferably more than a single line)

I've asked this with the same data one min ago.
