awk - GUI glitch using paste? -


this file:

file.txt

cg13869341      1       15865 cg24669183      1       534242 cg15560884      1       710097 cg01014490      1       714177 cg17505339      1       720865 cg11954957      1       758829 cg23803172      1       763119 cg16736630      1       779995 cg00168193      1       790667 cg05898754      1       805102 

awk '{print $2 "\t" $3 "\t" $3 "\t" $1}' file.txt

output

 1       cg13869341  1       cg24669183  1       cg15560884  1       cg01014490  1       cg17505339  1       cg11954957  1       cg23803172  1       cg16736630  1       cg00168193  1       cg05898754 

awk '{print $2 "\t" $3 "\t" $3 "\t" $1}' file.txt | head -1 | tr '\t' '\n'

output

 1  15865  15865  cg13869341 

ok, format inherently correct, output strange. try else.

awk '{print $1}' file.txt > 1.txt awk '{print $2}' file.txt > 2.txt awk '{print $3}' file.txt > 3.txt

paste 2.txt 3.txt 3.txt 1.txt | head

 1       cg13869341  1       cg24669183  1       cg15560884  1       cg01014490  1       cg17505339  1       cg11954957  1       cg23803172  1       cg16736630  1       cg00168193  1       cg05898754 

pasting 2.txt 3.txt gives expected output (cut head -2):

 1       15865  1       534242 

as 3.txt , 1.txt:

 15865   cg13869341  534242  cg24669183 

so why when paste 2.txt 3.txt 3.txt 1.txt, middle 2 columns disappear?

am missing here?

i can reproduce behavior file has windows line endings (\r\n instead of \n). in case, last field not "15865" "15865\r", every time $3 printed, cursor moved beginning of line before next tab , field printed. next field overwrites written $3, or part of if shorter.

you can convert file unix line endings number of tools such fromdos, dos2unix or recode. way on fly in awk is

awk '{ sub(/\r$/, ""); print $2 "\t" $3 "\t" $3 "\t" $1 }' file 

style note: instead of hard-coding separator, consider using ofs special variable:

awk -v ofs='\t' '{ sub(/\r$/, ""); print $2, $3, $3, $1 }' file 

that makes command easier adapt in case want generate differently separated values later.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -