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?
Prev   Next
# 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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question