awk to add/subtract an integer to/from each entry in columns?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk to add/subtract an integer to/from each entry in columns?
# 1  
Old 01-02-2014
awk to add/subtract an integer to/from each entry in columns?

---------- Post updated at 01:58 PM ---------- Previous update was at 01:48 PM ----------

For some reason my question is not getting printed. Here are the details:


I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk but can't think of how.

Example Input:
101	110
105	150
199	300
445	500

I want to add/subract 100 for each entry

Example Output:
1	210
5	250
99	400
345	600

# 2  
Old 01-02-2014
awk -v s=100 '{print $1-s, $2+s}' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-02-2014
Probably not the best code in the world, but I am still learning AWK:

 awk -v n=100 '{for(i=1;i<=NF;i++)printf(i%2?$i-n:$i+n)"\t"};{print FS}' file

Change n to the number you want to add/subtract. It will subtract for the first column, add for the second, subtract for the third, etc.

Last edited by pilnet101; 01-02-2014 at 05:11 PM..
# 4  
Old 01-02-2014
awk '{$1-=100;$2+=100}1' OFS='\t' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to subtract columns from 2 files and output to new file

Hi, I have 2 files in below formats File1_Stored.txt ABC:100, 83 ABC:84, 53 ABC:14, 1222And File2_Stored.txt ABC:100 , 83 ABC:84 , 1553 ABC:524 , 2626I am trying to get the 3rd file in below format. So, whenever difference is 0 it shouldn't appear but if the difference is not 0 then... (2 Replies)
Discussion started by: Abhayman
2 Replies

2. Shell Programming and Scripting

Add new column which is subtract of 2 columns.

Hi below is a file Date Category Time Attempts Success 2/17/2014 PayFlow ATB 0.999988 4039104 4039057 2/18/2014 PayFlow ATB 0.999912 4620964 4620558 2/19/2014 PayFlow ATB 0.999991 4380836 4380796 2/20/2014 PayFlow ATB 0.999988 5031047 5030985 2/21/2014 ... (5 Replies)
Discussion started by: villain41
5 Replies

3. Shell Programming and Scripting

Changing integer columns to floating decimal columns

I have a document that has 7 columns. eg. $1 $2 $3 $4 $5 $6 $7 string string string string integer integer integer The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all... (3 Replies)
Discussion started by: kadm
3 Replies

4. Shell Programming and Scripting

Add values in 2 columns and subtract from third

Hi All, I have a file with thousands of lines in the following format, where Field1=First 8 characters Field2-9-16 characters Field3=17-26 characters I need to add Field 1 and Field2 and subtract the result from Field 3. Field3=Field3 - (Field1 + Field2) 0012.00 0010.00 0001576.53... (4 Replies)
Discussion started by: nua7
4 Replies

5. Shell Programming and Scripting

Help need to subtract the data from 2 columns

space_used.lst /dev/vx/dsk/A06487-S01-c4e3s-ORACLE-dg/oradata01 505G 318G 175G 65% /dborafiles/nethealth21/PV/oradata01 /dev/vx/dsk/A06487-S01-c4e3s-ORACLE-dg/oradata02 505G 433G 67G 87% /dborafiles/nethealth21/PV/oradata02 /dev/vx/dsk/A06487-S01-c4e3s-ORACLE-dg/oradata03 507G 422G 79G 85%... (4 Replies)
Discussion started by: sathik
4 Replies

6. Shell Programming and Scripting

add and subtract specific row using awk

Hi Folks I have tried awk command to conditionally add or subtract value from specific row in a file. The test file looks like: # cat test.txt cont x y Max 0.3 0.9 Min 0.2 0.9 diff 0.1 0 # awk '{for (i=2; i <=NF; i++) if ($i== "0") $i=$i+0.2; print}' test.txt Output: cont... (1 Reply)
Discussion started by: dixits
1 Replies

7. Shell Programming and Scripting

How to subtract a number from all columns?

Hi, I want to subtract a number from all columns except the first column. I have a number of files each having different columns around 60/70. How to do that in awk or any other command? Thanks Input Col 1 Col 2 Col3 - - - - Col55 1 .0123 .098 - - - 0.6728 2 - -... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

8. Shell Programming and Scripting

Need help to subtract columns from 2 files and output to new file

Hi, I need some help figuring this out, I think it can be done using awk but I don't know how. So, I want to take two input files, subtract some columns with each other and then output to a new results file. InFile1.txt AAA 100 200 BBB CCC 300 400 DDD InFile2.txt AAA 50 60 BBB CCC 70... (7 Replies)
Discussion started by: MrTrigger
7 Replies

9. Shell Programming and Scripting

AWK solution to subtract multiple columns of numbers

Hope somebody is happy. NR==1 { num_columns=split( $0, menuend ); next; } { split( $0, substrend ); for ( i=1; i<=NF; i++ ) { minuend -= substrend; } } END { print "Result:"; for ( i=1; i<=num_columns; i++ ) { printf(... (3 Replies)
Discussion started by: awkward
3 Replies

10. UNIX for Dummies Questions & Answers

Add comma to integer using AWK

Srr for being pain her let say i have a data in a file like this 1@1000 2@2000 4@4000 5@7770 6@8998 7@80008 i am a newbie in Unix i need to add a comma to integer using AWK function. for example, 1,000 or 80,008 how can i do that ps. i'm using bash shell (1 Reply)
Discussion started by: Nutter
1 Replies
Login or Register to Ask a Question