Subtract 1 from all columns except columns 1 and 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtract 1 from all columns except columns 1 and 2
# 1  
Old 12-23-2013
Subtract 1 from all columns except columns 1 and 2

I have a file which has all integer numbers like this. This file is sort of very large, and I am only showing the first few contents. This representation can be regarded as a matrix of integer numbers.

Code:
14998
16 0 11680 3165 15343 8553 9925 3875 820 6430 14226 13261 11355 15428 9140 16184 7934 217
67 0 3165 11680 3884 12846 15343 5982 6486 9925 14226 11355 7933 17900 16131 12057 13112 3875 17447 820 6430 9140 5982 5982 3189 10371 5511 7652 6557 13942 10371 10095 5977 17176 1341 10095 1798 3093 5977 98 00 5982 10371 12288 15999 5411 5936 5036 9273 13312 4535 8594 3575 10371 6693 14448 10095 5988 6268 12095 5988 12711 10095 5826 4404 11311 14344 2483 2672 10067

I want to subtract 1 from all the columns except the first and the second column. As you can notice that the number of columns is not fixed. The second line has more number of columns than the first.

I can print all columns except the first and the second using this command:
Code:
awk '{$1=$2=""; print $0}' matrix.txt

What I found it difficult to do is how to subtract 1 from all the numbers obtained from the above command. I tried doing this:
Code:
awk '{$1=$2=""; print $0-1}' matrix.txt

It did not give me the results that I desire. I am using Linux with BASH.

---------- Post updated at 12:03 PM ---------- Previous update was at 11:05 AM ----------

I could solve this problem myself. This is what I did. In case there is a problem with this code, kindly let me know:
Code:
awk -v val="1" '{printf $1 " " $2 " "; for (i=3;i<=NF;i++) { printf ("%s ",$i-val)} printf ORS}' matrix.txt


Last edited by shoaibjameel123; 12-23-2013 at 12:13 AM..
# 2  
Old 12-23-2013
Try :
Code:
$ cat file
14998
16 0 11680 3165 15343 8553 9925 3875 820 6430 14226 13261 11355 15428 9140 16184 7934 217
67 0 3165 11680 3884 12846 15343 5982 6486 9925 14226 11355 7933 17900 16131 12057 13112 3875 17447 820 6430 9140 5982 5982 3189 10371 5511 7652 6557 13942 10371 10095 5977 17176 1341 10095 1798 3093 5977 98 00 5982 10371 12288 15999 5411 5936 5036 9273 13312 4535 8594 3575 10371 6693 14448 10095 5988 6268 12095 5988 12711 10095 5826 4404 11311 14344 2483 2672 10067

$ awk '{for(i=3;i<=NF;i++)$i-=val}1' val=1 file

14998
16 0 11679 3164 15342 8552 9924 3874 819 6429 14225 13260 11354 15427 9139 16183 7933 216
67 0 3164 11679 3883 12845 15342 5981 6485 9924 14225 11354 7932 17899 16130 12056 13111 3874 17446 819 6429 9139 5981 5981 3188 10370 5510 7651 6556 13941 10370 10094 5976 17175 1340 10094 1797 3092 5976 97 -1 5981 10370 12287 15998 5410 5935 5035 9272 13311 4534 8593 3574 10370 6692 14447 10094 5987 6267 12094 5987 12710 10094 5825 4403 11310 14343 2482 2671 10066

This User Gave Thanks to Akshay Hegde For This Post:
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. UNIX for Dummies Questions & Answers

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: Greetings. I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk... (3 Replies)
Discussion started by: Twinklefingers
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

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

6. Shell Programming and Scripting

Subtract 2 date columns in .csv file and get output as number of days

Hi, I have one .csv file. I have 2 date columns present in file, column 2 and column 3. I need to calculate how many days exist between 2 dates. I am trying to subtract date column 2 from date column 3. Eg: my file look likes s.no, Start_date,End_Date 1, 7/29/2012,10/27/2012 2,... (9 Replies)
Discussion started by: Dimple
9 Replies

7. 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

8. 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

9. 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

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