How to add a column numbers at a particular position?

Problem discription:
I have many files which contain the same lines.
for instance, (15 lines) file1 ..last column add by hand arbitrarily.

1.78116800    0.68396600    0.00061900
 0.47641600   -0.49794500   -0.00024000
-1.70662800    0.29577100    0.67863600
-1.70647600    0.29654600   -0.67839300
-2.11640600   -0.53962500    1.24614600
-2.11614700   -0.53818900   -1.24695300
-1.41397300    1.17775000   -1.24768500
-1.41421300    1.17630200    1.24901200
 1.85604200    1.64880200   -0.00117100

Now, I'm trying to add a column numbers at the end of each line after a space. By this way, It will become as follows,

1.78116800    0.68396600    0.00061900 1
 0.47641600   -0.49794500   -0.00024000 1
-1.70662800    0.29577100    0.67863600 2
-1.70647600    0.29654600   -0.67839300 2
-2.11640600   -0.53962500    1.24614600 2
-2.11614700   -0.53818900   -1.24695300 2
-1.41397300    1.17775000   -1.24768500 2
-1.41421300    1.17630200    1.24901200 2
 1.85604200    1.64880200   -0.00117100 1

For instance, put the last column 1 or 2 at the end of the following file...file2

0.57531500    2.12664000    0.00000000
0.00000000    0.45595600    0.00000000
-0.96804600   -1.82077600    0.00000000
0.37135000   -2.02000800    0.00000000
-1.53721100   -1.80824400    0.92977300
0.92534800   -2.15667300   -0.92822100
0.92534800   -2.15667300    0.92822100
-1.53721100   -1.80824400   -0.92977300
0.20138500    3.01847100    0.00000000

I use VISUAL BLOCK mode to cut the last column by pressing "x" and add it to the new file by pressing "p".
It is so boring to do the same work for hundreds times.
Is that possible to write a script to do this job?
For example,
I have five files named file1 file2 file3 file4 file5. I add a column of number to file1, how to add the same column to other files?
Any suggestion will be greatly appreciated. Thanks in advance!

zhen from shanghai

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

****************************************************

I edited your post and added CODE tags as you can see. How do you decide if the number you add is 1 or 2?

I'm a new guy for this high quality forums. Sorry for the bad style of my post.

The first column numbers added to the end of each line have beed done by hand.

But I still have dozens of files need to add the same column.
I used the Visual Block mode by pressing 'x' and the add this column to a new file by pressing 'p'.

Now, I would like to know if some script can be made to do this work?

Thank you very much!

What do you mean with the first column numbers?

In your first post you added a column with a 1 or a 2, if that's the expected output, then what is the condition to place a 1 or a 2?

Clarify your question.

Actually, these numbers ('1' or '2') were difined by myselt to show two different fragments. So for the first input file, I must add this column numbers by hand.

Then I try to copy this column numbers into other files, which will be put into the same position.
For instance, this column will be put start at the end of line 9.

I hope I could make me much clear about this question.

thanks..

just a WILD guess:

nawk '
{
  for(i=1;i<=NF;i++)
    if (int($i)>=0) {
       $(++NF)=i
       break
    }
    if (i>NF) $(++NF)=0
    print
}' myFile

Naaaah, this is not what the OP wants - the explanation is still not clear.

Assuming file1 has the new columns and you want these columns in file2 in the order of file1:

awk 'NR==FNR{a[NR]=$NF;next}{print $0 FS a[FNR]}' file1 file2

Simply to say, I just want to use a script to copy the last column numbers from the the first file added by hand to the other files which contain the same lines.
:o

bash 4

#!/bin/bash
exec 6<"file1"
exec 7<"file2"
while read -a arr <&6 
do
    read line <&7
    echo "$line ${arr[@]:(-1)}" 
done
exec >&6-
exec >&7-

.... just another WILD guess :cool:

awk '{$0=$0 FS (($1>=0)?1:2)}1' file