This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to add column to table with awk

Hi Biostars community

I have a file with positions of methylated cytosines , but I want to add a new column to this file with the position+1 value (as if it was a dinucleotide).

I have used awk to print our the new value, but I need to add this new column to my file.

My file looks like this

NC_001960.1 1067
NC_001960.1 1068
NC_001960.1 1069
NC_001960.1 1133

And I do this with awk: awk -v s=1 '{print $(NF)+s}' test Which gives me this output:

1068

1069

1070

1134

And I´d like to add this as the third column in the above file

NC_001960.1 1067 1068
NC_001960.1 1068 1069
NC_001960.1 1069 1070
NC_001960.1 1133 1134

Is it possible to do this with awk or with some other method?

awk bash shell unix bed-files

2 answers

You can just slightly modify your awk command.

awk -v s=1 '{print $0,$(NF)+s}' test

NC_001960.1 1067 1068
NC_001960.1 1068 1069
NC_001960.1 1069 1070
NC_001960.1 1133 1134

Allright, that was so easy I´m almost embarrassed :) thanks a bunch!

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

Yes, it's possible to append a column in the using the code below:

awk -v s=1 '{print $0,$(NF)+s}' test

Where test file is

NC_001960.1 1067

NC_001960.1 1068

NC_001960.1 1069

NC_001960.1 1133

The output of the code is:

NC_001960.1 1067 1068

NC_001960.1 1068 1069

NC_001960.1 1069 1070

NC_001960.1 1133 1134

thank you for taking time answering me!

Log in to answer this question.