Subtracting each row from the first row in a single column file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtracting each row from the first row in a single column file using awk
# 8  
Old 04-04-2012
Thanks Carlo and Scrutinizer..

Both of your scripts are perfectly working on my data.

Thanks a ton for both of you.

Sid..

---------- Post updated at 11:49 PM ---------- Previous update was at 11:29 PM ----------

I have some files(.csv) with the following data(Example) in them.
Code:
A,1,X
B,2,Y
C,3,Z

Now I need the following output..
Code:
A,1,X
B,1,Y
C,2,Z

Where my second column got formatted as for my condition
(row1-row1)
(row2-row1)
(row3-row1)
...
So on.. and leaving all the other columns undisturbed.

I am really fond of using awk but still learning. so any help in this regard is highly appreciated.

Thanks
Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-04-2012 at 03:57 PM.. Reason: code tags
# 9  
Old 04-04-2012
Quote:
Originally Posted by ks_reddy
I need the following operation to be done.
(Row1-Row1)
(Row2-Row1)
(Row3-Row1)
...
So on..
The script "awk '$1--{print}'" is not giving correct output.
For the input of
Code:
1.1
2.5
3.2
4
5
6

I got the output as
Code:
0.1
1.5
2.2
3
4
5

which is not correct..
Can you please modify the script to match requirement.

Thanks
Sidda
I thought all your numbers are integers ,have this code revised

Code:
awk '$1=$1-1{print}' sample_file


Last edited by Scrutinizer; 04-04-2012 at 03:58 PM.. Reason: Nothing changed, wrong post..
# 10  
Old 04-04-2012
Quote:
Originally Posted by ks_reddy
[..]I have some files(.csv) with the following data(Example) in them.
Code:
A,1,X
B,2,Y
C,3,Z

Now I need the following output..
Code:
A,1,X
B,1,Y
C,2,Z

Where my second column got formatted as for my condition
(row1-row1)
(row2-row1)
(row3-row1)
...
So on.. and leaving all the other columns undisturbed.

I am really fond of using awk but still learning. so any help in this regard is highly appreciated.

Thanks
Use FS=, OFS=, and change $1 to $2 and Bob's your uncle..
Code:
awk 'NR==1{v=$2}{$2-=v}1' FS=, OFS=, infile

--
PS. Please, please heed the code tags warnings. Check your forum inbox

Last edited by Scrutinizer; 04-04-2012 at 04:56 PM..
# 11  
Old 04-04-2012
Scrutinizer ,

Code:
awk '$1=$1-1{print}' sample_file

perfectly decrements value by one , have this tested taking decimal numbers as stated in the post .
# 12  
Old 04-04-2012
Quote:
Originally Posted by codemaniac
perfectly decrements value by one , have this tested taking decimal numbers as stated in the post .
The OP wants to decrement by the value of row 1, not just literal 1 (although the title and initial description say something else Smilie).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

3. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

4. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

5. Shell Programming and Scripting

How to merge multiple rows into single row if first column matches ?

Hi, Can anyone suggest quick way to get desired output? Sample input file content: A 12 9 A -0.3 2.3 B 1.0 -4 C 34 1000 C -111 900 C 99 0.09 Output required: A 12 9 -0.3 2.3 B 1.0 -4 C 34 1000 -111 900 99 0.09 Thanks (3 Replies)
Discussion started by: cbm_000
3 Replies

6. Shell Programming and Scripting

duplicate row based on single column

I am a newbie to shell scripting .. I have a .csv file. It has 1000 some rows and about 7 columns... but before I insert this data to a table I have to parse it and clean it ..basing on the value of the first column..which a string of phone number type... example below.. column 1 ... (2 Replies)
Discussion started by: mitr
2 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

Concatenating column values with unique id into single row

Hi, I have a table in Db2 with data say id_1 phase1 id_1 phase2 id_1 phase3 id_2 phase1 id_2 phase2 I need to concatenate the values like id_1 phase1,phase2,phase3 id_2 phase1,phase2 I tried recursive query but in vain as the length of string to be concatenated in quite long. ... (17 Replies)
Discussion started by: jsaravana
17 Replies

9. Shell Programming and Scripting

Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file. eg: Input file: ColA ColB 1 2 2 x 3 y... (5 Replies)
Discussion started by: nvuradi
5 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question