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

Hi Friends,

I have a single column data like below.

1
2
3
4
5

I need the output like below.

0
1
2
3
4

where each row (including first row) subtracting from first row and the result should print below like the way shown in output file.

Thanks
Sid

That would do the trick .

awk '$1--{print}' sample_file

You want row 2 (etc) subtracted from row 1, or row 1 subtracted from row 2?

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

1.1
2.5
3.2
4
5
6

I got the output as

0.1
1.5
2.2
3
4
5

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

Thanks
Sidda

try:

awk 'NR==1{v=$1}{$1-=v}1' infile

But What I need is

"Row1-Row1"
"Row2-Row1"
"Row3-Row1"
"Row4-Row1"
.....
so on
in the output file.( whatever may be the input numeric data single column file)

Regards
Sid

Try

awk 'FNR==1 {p=$1} {print $1-p}' file

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.

A,1,X
B,2,Y
C,3,Z

Now I need the following output..

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

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

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

Use FS=, OFS=, and change $1 to $2 and Bob's your uncle..

awk 'NR==1{v=$2}{$2-=v}1' FS=, OFS=, infile

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

Scrutinizer ,

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

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 :p).