Myke
Myke

Reputation: 63

awk - Duplicate, Rearrange and Add columns to file

I would like to :

  1. duplicate 2 columns

  2. switch position between columns

  3. add a variable (as: 0,128,128) in a column at the end of the file

My file :

chr1    3006607 3006623 Class   0   +
chr1    3006607 3006623 Class   0   +
chr1    3006607 3006623 Class   0   +
chr1    3006607 3006623 Class   0   +
chr1    3006607 3006623 Class   0   +
....continue

My code :

cat FILE.txt | awk 'BEGIN { FS=" "; OFS="\t" } { print $1, $2=$2 "\t" $2, $3=$3 "\t" $3, $4, $5, $6 }' | awk 'BEGIN { FS="\t"; OFS="\t" } { print $1, $2, $4, $6, $7, $8, $3, $5 "\t" "0,128,128" }' > FILE.bed

My out-put :

chr1    3006607 3006623 Class   0   +
        3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +
        3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +
        3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +
        3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +
        3006607 3006623 0,128,128
...continue

ERROR = DUPLICATED COLUMNS AND THE ADDED ONE ARE IN A ROW BELOW

What I would like to obtain !

chr1    3006607 3006623 Class   0   +  3006607  3006623   0,128,128
chr1    3006607 3006623 Class   0   +  3006607  3006623   0,128,128
chr1    3006607 3006623 Class   0   +  3006607  3006623   0,128,128
chr1    3006607 3006623 Class   0   +  3006607  3006623   0,128,128
chr1    3006607 3006623 Class   0   +  3006607  3006623   0,128,128
.....continue

What am I doing wrong? Am I missing NR or paste0?

Upvotes: 1

Views: 100

Answers (2)

Ed Morton
Ed Morton

Reputation: 204731

If the code in your question produces output that puts the duplicated values on a new row then your input has DOS line endings (or similar?) causing that to happen because your code will not do that.

See Why does my tool output overwrite itself and how do I fix it? for how to handle DOS line endings and then this is all you really need instead of the script in your question:

$ awk -v OFS='\t' '{print $0, $2, $3, "0,128,128"}' file
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128

Upvotes: 1

anubhava
anubhava

Reputation: 786359

You may use:

awk -v OFS='\t' -v s='0,128,128' '{$1=$1; print $0, $2, $3, s}' file

chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128
chr1    3006607 3006623 Class   0   +   3006607 3006623 0,128,128

$1=$1 is to force $0 to be reformatted with tab as field separator.

Upvotes: 1

Related Questions