How to add column to table with awk
2
0
Entering edit mode
4.4 years ago
linelr ▴ 40

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 • 7.3k views
ADD COMMENT
3
Entering edit mode
4.4 years ago

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
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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

ADD REPLY
2
Entering edit mode
4.4 years ago
Shalu Jhanwar ▴ 540

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

ADD COMMENT
0
Entering edit mode

thank you for taking time answering me!

ADD REPLY

Login before adding your answer.

Traffic: 2288 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6