Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large file like this

AT5G44030,TAACARA,1000,+,200,206
AT5G44030,TAACARA,1000,+,418,424
AT5G44030,TAACARA,1000,+,773,779
AT5G44030,NGATT,1000,+,114,118
AT5G44030,NGATT,1000,+,267,271
....................................

I want to add two columns at the end with subtractions of 5th - 3rd , 6th - 3rd so the final file should look like follows.

AT5G44030,TAACARA,1000,+,200,206,-800,-794
AT5G44030,TAACARA,1000,+,418,424,-582,-576
AT5G44030,TAACARA,1000,+,773,779,-227,-221
AT5G44030,NGATT,1000,+,114,118,-886,-882
AT5G44030,NGATT,1000,+,267,271,-733,-729

.......................................

(note that there are no empty lines in between)

how do i do this using awk or sed ? the file is very large 1M lines.

so I couldn't get a better script to do it using perl

I am new to awk and sed can anyone please help ?

Thank you

share|improve this question

awk 'BEGIN { FS=OFS="," } {print $0,$5-$3,$6-$3}' my_file

share|improve this answer
2  
or set OFS="," to avoid having to manually add "," between each new field: awk 'BEGIN { FS=","; OFS=","} {print $0, $5-$3, $6-$3}' – cas 3 hours ago
    
@cas you're right, thanks! – Guido 3 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.