anikaM
anikaM

Reputation: 429

How to duplicate a column?

I have a data out3 looking like this:

ENSG00000133789 11 9554976
ENSG00000131626 11 70272382
ENSG00000020922 11 94324686
ENSG00000102710 13 36925520
...

I would like to duplicate the 3rd column and get this:

ENSG00000133789 11 9554976 9554976
ENSG00000131626 11 70272382 70272382
ENSG00000020922 11 94324686 94324686
ENSG00000102710 13 36925520 36925520
...

I tried doing this:

cat out3 | awk '{ $3=$4; print; }'

but I got:

ENSG00000133789 11 
ENSG00000131626 11 
ENSG00000020922 11 
ENSG00000102710 13 
...

My columns are space separated. How do I make this work?

EDIT:

I solved it via:

awk -F' ' '{print $0" "$3}' out3 > out4

Upvotes: 2

Views: 3917

Answers (2)

karakfa
karakfa

Reputation: 67507

in general, to duplicate the last column

$ awk '{print $0,$NF}' file

Upvotes: 9

JRFerguson
JRFerguson

Reputation: 7516

awk '$4=$3' out3

Is all that's necessary. Note that awk accepts its input from STDIN and hence piping to awk from a cat is wasteful of a process and superfluous. print is the default awk action, so it can be omitted for brevity.

Upvotes: 4

Related Questions