Visit Our UNIX and Linux User Community


How to add a column numbers at a particular position?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add a column numbers at a particular position?
# 1  
Old 10-20-2009
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.
Code:
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,
Code:
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
Code:
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

Last edited by liuzhencc; 10-20-2009 at 10:07 AM.. Reason: code tags
# 2  
Old 10-20-2009
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 [code] and [/code] 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?
# 3  
Old 10-20-2009
Thanks for your suggestion!

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!
# 4  
Old 10-20-2009
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.
# 5  
Old 10-20-2009
Sorry for my poor expression...

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..
# 6  
Old 10-20-2009
just a WILD guess:
Code:
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.
# 7  
Old 10-20-2009
Assuming file1 has the new columns and you want these columns in file2 in the order of file1:

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


Previous Thread | Next Thread
Test Your Knowledge in Computers #162
Difficulty: Easy
Early versions of MINIX were created by Andrew S. Tanenbaum for commercial purposes.
True or False?

10 More Discussions You Might Find Interesting

1. Linux

Removing a character at specific position in a column

Hi, I have a file like this (about 8 columns in total, this being the 2nd column) gi_49482297_ref_YP_039521.1_ gi_49482297_ref_YP_039521.1_ gi_49482315_ref_YP_039539.1_ gi_49482315_ref_YP_039539.1_I want to remove the _ at the end of the line. And at later stages I would want to replace the... (5 Replies)
Discussion started by: Syeda Sumayya
5 Replies

2. Shell Programming and Scripting

Identify the First Column Position in Second Column and add the position value

Identify the First Column Position in Second Column and add the position value in 3rd column. Sample data: a|c b|d c|a d|b e|e f|g g|f |h |i Expected Output: a|c|1 b|d|2 c|a|3 d|b|4 (6 Replies)
Discussion started by: BrahmaNaiduA
6 Replies

3. Shell Programming and Scripting

Trying to move a column into another position within a sed script

Currently the table looks like this student-id,last,first,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2 pts-avail,,,100,150,100,200,100,150,300 991-78-7872,Thompson,Ken,95,143,79,185,95,135,259 123-45-6789,Richie,Dennis,99,123,89,189,97,139,279 234-56-7891,Aho,Al,78,146,75,176,88,128,285... (3 Replies)
Discussion started by: ertang
3 Replies

4. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

5. Shell Programming and Scripting

Add up a column of numbers

Given a file test.txt ,I can get a list of numbers in a single column using the command : cat test.txt | cut -d ' ' -f 8 that gives the output as 52 52 52 60 52 How can I get the sum of all the numbers in that column that is displayed? i want the output as sum=268 (4 Replies)
Discussion started by: hitha87
4 Replies

6. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

7. Shell Programming and Scripting

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

8. Shell Programming and Scripting

Merge group numbers and add a column containing group names

I have a file in the following format. Groups of data merge together and the group number is indicated above each group. 1 adrf dfgr dfg 2 dfgr dfgr 3 dfef dfr fd 4 fgrt fgr fgg 5 fgrt fgr (3 Replies)
Discussion started by: Lucky Ali
3 Replies

9. Shell Programming and Scripting

How to add numbers in a column

Hi All thanks a lot for your previous replies. I need some help here. I am writing a script to test a machine for a thereshold. It is genrating the list of number that have to be added but not displaying the added value. The script is like this #!/bin/sh... (1 Reply)
Discussion started by: asirohi
1 Replies

10. Shell Programming and Scripting

File Manipulation (Move Column Position)

Hi All, I have a comma separated value (.CSV) file like the one below. The file contains about 20000 lines. FileName EmpNo,Name,Age,Sex,Band,Spouse,Children,Salary, 1000,Arnold,24,M,B,N.A.,No,10000, 1001,Jenny,27,F,C,John,2,20000, ................................... What i need is to... (1 Reply)
Discussion started by: ultimate
1 Replies

Featured Tech Videos