how to add a constant value to a column in a file using unix command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to add a constant value to a column in a file using unix command
# 1  
Old 01-31-2011
how to add a constant value to a column in a file using unix command

I have a file like this

1 chr1 3661579 3662579
2 chr1 4350395 4351395
3 chr1 4399322 4400322
4 chr1 4486494 4487494
5 chr1 4775807 4776807
6 chr1 4775807 4776807
7 chr1 4775807 4776807
8 chr1 4796973 4797973
9 chr1 4846774 4847774
10 chr1 4846774 4847774
11 chr1 4847408 4848408
12 chr1 5009507 5010507
13 chr1 5060366 5061366
14 chr1 5072253 5073253
15 chr1 5577573 5578573
16 chr1 5907479 5908479

for the forth column , I d like to add 1000 to each value, does anyone know the linux command for that. Thank you so much, I really appreciate.
# 2  
Old 01-31-2011
Code:
#!/bin/bash
while read A B C D
do
        echo $A $B $C $((D+1000))
done < inputfile > outputfile

# 3  
Old 01-31-2011
Another one:
Code:
awk '{$4+=1000}1' file > newfile

# 4  
Old 01-31-2011
Or even
Code:
awk '$4+=1000' file


Last edited by Scrutinizer; 01-31-2011 at 05:26 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to add a new column entry

My input file looks like this 12 3 5.122.281.413 172.31.15.220 3421 4133 2 2 1454 3421 4133 2 2 0 12 44036 214.215.52.146 90.123.245.211 2312 3911 4 4 521 2312 3911 4 4 1 14 504 6.254.324.219 192.61.27.120 4444 5611 7 5 1415 4444 5611 7 5 1 ... (2 Replies)
Discussion started by: sampitosh
2 Replies

2. Shell Programming and Scripting

UNIX command -Filter rows in fixed width file based on column values

Hi All, I am trying to select the rows in a fixed width file based on values in the columns. I want to select only the rows if column position 3-4 has the value AB I am using cut command to get the column values. Is it possible to check if cut -c3-4 = AB is true then select only that... (2 Replies)
Discussion started by: ashok.k
2 Replies

3. UNIX for Advanced & Expert Users

Copy a column to another column in UNIX fixedwidth file

Hi All, I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 – 1171 to the same file. Please let me know how can i achive using a single command in fixed width file. Also source column length is 4 chars and target column length... (4 Replies)
Discussion started by: kiranparsha
4 Replies

4. Shell Programming and Scripting

Howto add a constant column to the text file

Hi, I am converting a .DBF file to pipe delimited file my requirement is like lets say my .DBF is residing in path /a/b/c/d/f/abc.DBF I need my .txt file as having a column with source _cd =f sample data in .DBF in folder "f" c1 c2 c3 1 2 3 in txt file it should be... (4 Replies)
Discussion started by: angel12345
4 Replies

5. Shell Programming and Scripting

how I can add a constant to a field without changing the file format

Hi, I need to edit a file Protein Data Bank (pdb) and then open that file with the program VMD but when I edit the file with awk, it changes pdb format and the VMD program can not read it. I need to subtract 34 to field 6 ($ 6). this is a pdb file : ATOM 918 N GLY B 103 -11.855 8.675... (8 Replies)
Discussion started by: bio_
8 Replies

6. Shell Programming and Scripting

Add to constant fields at the end of every line

Hi, I want to add two fields with values '1000' and 'XYZ-1234' at the end of every line in a comma delimited file. Should I use any command in a loop to add the fields or using any single command Shall I acheive it? Kindly help me in code. Thanks, Poova. (6 Replies)
Discussion started by: poova
6 Replies

7. Shell Programming and Scripting

How to get the first column from the txt file using unix command?

Hi All, I have the file like this (file name is : tem_text) no Id name ccy ------- ---- ------------------- -------- 7777 17 India Overseas Partners 500INR I want to retreive the third colimn of from the above text file if i use the basic awk command cat... (4 Replies)
Discussion started by: psiva_arul
4 Replies
Login or Register to Ask a Question